MIDAS
Loading...
Searching...
No Matches
mtfe.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: mtfe.c
4 Created by: Stefan Ritt
5
6 Contents: Multi-threaded frontend example.
7
8 $Id: frontend.c 4089 2007-11-27 07:28:17Z ritt@PSI.CH $
9
10\********************************************************************/
11
12#include <stdio.h>
13#include <stdlib.h>
14#include "midas.h"
15#include "msystem.h"
16
17#include "mfe.h"
18
19/*-- Globals -------------------------------------------------------*/
20
21/* The frontend name (client name) as seen by other MIDAS clients */
22const char *frontend_name = "Sample Frontend";
23/* The frontend file name, don't change it */
25
26/*-- Function declarations -----------------------------------------*/
27
29
30/*-- Equipment list ------------------------------------------------*/
31
33
35
36 {"Trigger", /* equipment name */
37 {1, 0, /* event ID, trigger mask */
38 "SYSTEM", /* event buffer */
39 EQ_USER, /* equipment type */
40 0, /* event source (not used) */
41 "MIDAS", /* format */
42 TRUE, /* enabled */
43 RO_RUNNING, /* read only when running */
44 500, /* poll for 500ms */
45 0, /* stop run after this event limit */
46 0, /* number of sub events */
47 0, /* don't log history */
48 "", "", "",},
49 NULL, /* readout routine */
50 },
51
52 {""}
53};
54
55/*-- Frontend Init -------------------------------------------------*/
56
58{
59 /* for this demo, use three readout threads */
60 for (int i=0 ; i<3 ; i++) {
61
62 /* create a ring buffer for each thread */
64
65 /* create readout thread */
67 }
68
69 return SUCCESS;
70}
71
72/*-- Event readout -------------------------------------------------*/
73
75{
76 EVENT_HEADER *pevent;
77 WORD *pdata, *padc;
78 int index, i, status, exit = FALSE;
79 INT rbh;
80
81 /* index of this thread */
82 index = (int)(PTYPE)param;
83
84 /* tell framework that we are alive */
86
87 /* set name of thread as seen by OS */
88 ss_thread_set_name(std::string(equipment[0].name) + "RT" + std::to_string(index));
89
90 /* Initialize hardware here ... */
91 printf("Start readout thread %d\n", index);
92
93 /* Obtain ring buffer for inter-thread data exchange */
95
97
98 if (!readout_enabled()) {
99 // do not produce events when run is stopped
100 ss_sleep(10);
101 continue;
102 }
103
104 /* check for new event (poll) */
105 status = ss_sleep(100); // for this demo, just sleep a bit
106
107 if (status) { // if event available, read it out
108
109 // check once more in case state changed during the poll
111 break;
112
113 // obtain buffer space
114 do {
115 status = rb_get_wp(rbh, (void **) &pevent, 0);
116 if (status == DB_TIMEOUT) {
117 ss_sleep(10);
118 // check for readout thread disable, thread might be stop from main thread
119 // in case Ctrl-C is hit for example
121 exit = TRUE;
122 break;
123 }
124 }
125 } while (status != DB_SUCCESS);
126
127 if (exit)
128 break;
129
131 pdata = (WORD *)(pevent + 1);
132
133 /* init bank structure */
135
136 /* create ADC0 bank */
137 bk_create(pdata, "ADC0", TID_WORD, (void **)&padc);
138
139 /* just put in some random numbers in this demo */
140 int len = 32 + rand() % 10000;
141 for (i=0 ; i<len; i++)
142 *padc++ = len;
143
145
146 pevent->data_size = bk_size(pdata);
147
148 /* send event to ring buffer */
149 rb_increment_wp(rbh, sizeof(EVENT_HEADER) + pevent->data_size);
150 }
151 }
152
153 /* tell framework that we are finished */
155
156 printf("Stop readout thread %d\n", index);
157
158 return 0;
159}
#define FALSE
Definition cfortran.h:309
INT bk_close(void *event, void *pdata)
Definition midas.cxx:16780
void bk_init32(void *event)
Definition midas.cxx:16469
void bk_create(void *event, const char *name, WORD type, void **pdata)
Definition midas.cxx:16561
INT bk_size(const void *event)
Definition midas.cxx:16495
INT bm_compose_event_threadsafe(EVENT_HEADER *event_header, short int event_id, short int trigger_mask, DWORD data_size, DWORD *serial)
Definition midas.cxx:8292
#define DB_SUCCESS
Definition midas.h:631
#define DB_TIMEOUT
Definition midas.h:655
unsigned short int WORD
Definition mcstd.h:49
#define SUCCESS
Definition mcstd.h:54
#define EQ_USER
Definition midas.h:422
#define TID_WORD
Definition midas.h:332
#define RO_RUNNING
Definition midas.h:426
INT EXPRT ss_thread_set_name(std::string name)
Definition system.cxx:2426
midas_thread_t ss_thread_create(INT(*thread_func)(void *), void *param)
Definition system.cxx:2310
INT ss_sleep(INT millisec)
Definition system.cxx:3628
int rb_get_wp(int handle, void **p, int millisec)
Definition midas.cxx:17427
int rb_increment_wp(int handle, int size)
Definition midas.cxx:17502
INT index
Definition mana.cxx:271
char param[10][256]
Definition mana.cxx:250
INT i
Definition mdump.cxx:32
static int rbh[MAX_N_THREADS]
Definition mfe.cxx:71
void signal_readout_thread_active(int index, int flag)
Definition mfe.cxx:1180
int create_event_rb(int i)
Definition mfe.cxx:1150
int get_event_rbh(int i)
Definition mfe.cxx:1160
bool readout_enabled(void)
Definition mfe.cxx:1096
bool is_readout_thread_enabled()
Definition mfe.cxx:1168
DWORD BOOL
Definition midas.h:105
int INT
Definition midas.h:129
#define PTYPE
Definition midas.h:170
#define TRUE
Definition midas.h:182
#define serial_number
#define name(x)
Definition midas_macro.h:24
const char * frontend_file_name
Definition mtfe.cxx:24
INT trigger_thread(void *param)
Definition mtfe.cxx:74
INT frontend_init()
Frontend initialization.
Definition mtfe.cxx:57
BOOL equipment_common_overwrite
Definition mtfe.cxx:32
EQUIPMENT equipment[]
Definition mtfe.cxx:34
const char * frontend_name
Definition mtfe.cxx:22
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
DWORD data_size
Definition midas.h:856