source: libcfa/src/concurrency/actor.hfa@ 13f066d

ADT ast-experimental
Last change on this file since 13f066d was 1e38178, checked in by caparson <caparson@…>, 3 years ago

added some safety/productivity features and some stats

  • Property mode set to 100644
File size: 25.5 KB
Line 
1#pragma once
2
3#include <locks.hfa>
4#include <limits.hfa>
5#include <kernel.hfa>
6#include <time_t.hfa>
7#include <time.hfa>
8#include <iofwd.hfa>
9
10#ifdef __CFA_DEBUG__
11#define CFA_DEBUG( stmt ) stmt
12#else
13#define CFA_DEBUG( stmt )
14#endif // CFA_DEBUG
15
16// Define the default number of processors created in the executor. Must be greater than 0.
17#define __DEFAULT_EXECUTOR_PROCESSORS__ 2
18
19// Define the default number of threads created in the executor. Must be greater than 0.
20#define __DEFAULT_EXECUTOR_WORKERS__ 2
21
22// Define the default number of executor request-queues (mailboxes) written to by actors and serviced by the
23// actor-executor threads. Must be greater than 0.
24#define __DEFAULT_EXECUTOR_RQUEUES__ 4
25
26// Define if executor is created in a separate cluster
27#define __DEFAULT_EXECUTOR_SEPCLUS__ false
28
29#define __DEFAULT_EXECUTOR_BUFSIZE__ 10
30
31// #define __STEAL 0 // workstealing toggle. Disjoint from toggles above
32
33// workstealing heuristic selection (only set one to be 1)
34// #define RAND 0
35// #define SEARCH 0
36
37// show stats
38// #define STATS
39
40// forward decls
41struct actor;
42struct message;
43struct executor;
44
45enum Allocation { Nodelete, Delete, Destroy, Finished }; // allocation status
46
47typedef Allocation (*__receive_fn)(actor &, message &);
48struct request {
49 actor * receiver;
50 message * msg;
51 __receive_fn fn;
52 bool stop;
53};
54
55static inline void ?{}( request & this ) { this.stop = true; } // default ctor makes a sentinel
56static inline void ?{}( request & this, actor * receiver, message * msg, __receive_fn fn ) {
57 this.receiver = receiver;
58 this.msg = msg;
59 this.fn = fn;
60 this.stop = false;
61}
62static inline void ?{}( request & this, request & copy ) {
63 this.receiver = copy.receiver;
64 this.msg = copy.msg;
65 this.fn = copy.fn;
66 this.stop = copy.stop;
67}
68
69// Vector-like data structure that supports O(1) queue operations with no bound on size
70// assumes gulping behaviour (once a remove occurs, removes happen until empty beforw next insert)
71struct copy_queue {
72 request * buffer;
73 size_t count, buffer_size, index, utilized, last_size;
74};
75static inline void ?{}( copy_queue & this ) {}
76static inline void ?{}( copy_queue & this, size_t buf_size ) with(this) {
77 buffer_size = buf_size;
78 buffer = aalloc( buffer_size );
79 count = 0;
80 utilized = 0;
81 index = 0;
82 last_size = 0;
83}
84static inline void ^?{}( copy_queue & this ) with(this) { adelete(buffer); }
85
86static inline void insert( copy_queue & this, request & elem ) with(this) {
87 if ( count >= buffer_size ) { // increase arr size
88 last_size = buffer_size;
89 buffer_size = 2 * buffer_size;
90 buffer = realloc( buffer, sizeof( request ) * buffer_size );
91 /* paranoid */ verify( buffer );
92 }
93 memcpy( &buffer[count], &elem, sizeof(request) );
94 count++;
95}
96
97// once you start removing you need to remove all elements
98// it is not supported to call insert() before the array is fully empty
99static inline request & remove( copy_queue & this ) with(this) {
100 if ( count > 0 ) {
101 count--;
102 size_t old_idx = index;
103 index = count == 0 ? 0 : index + 1;
104 return buffer[old_idx];
105 }
106 request * ret = 0p;
107 return *0p;
108}
109
110// try to reclaim some memory if less than half of buffer is utilized
111static inline void reclaim( copy_queue & this ) with(this) {
112 if ( utilized >= last_size || buffer_size <= 4 ) { utilized = 0; return; }
113 utilized = 0;
114 buffer_size--;
115 buffer = realloc( buffer, sizeof( request ) * buffer_size ); // try to reclaim some memory
116}
117
118static inline bool isEmpty( copy_queue & this ) with(this) { return count == 0; }
119
120struct work_queue {
121 __spinlock_t mutex_lock;
122 copy_queue * owned_queue; // copy queue allocated and cleaned up by this work_queue
123 copy_queue * c_queue; // current queue
124 volatile bool being_processed; // flag to prevent concurrent processing
125 #ifdef STATS
126 unsigned int id;
127 size_t missed; // transfers skipped due to being_processed flag being up
128 #endif
129}; // work_queue
130static inline void ?{}( work_queue & this, size_t buf_size, unsigned int i ) with(this) {
131 owned_queue = alloc(); // allocated separately to avoid false sharing
132 (*owned_queue){ buf_size };
133 c_queue = owned_queue;
134 being_processed = false;
135 #ifdef STATS
136 id = i;
137 missed = 0;
138 #endif
139}
140
141// clean up copy_queue owned by this work_queue
142static inline void ^?{}( work_queue & this ) with(this) { delete( owned_queue ); }
143
144static inline void insert( work_queue & this, request & elem ) with(this) {
145 lock( mutex_lock __cfaabi_dbg_ctx2 );
146 insert( *c_queue, elem );
147 unlock( mutex_lock );
148} // insert
149
150static inline void transfer( work_queue & this, copy_queue ** transfer_to ) with(this) {
151 lock( mutex_lock __cfaabi_dbg_ctx2 );
152 #ifdef __STEAL
153
154 // check if queue is being processed elsewhere
155 if ( unlikely( being_processed ) ) {
156 #ifdef STATS
157 missed++;
158 #endif
159 unlock( mutex_lock );
160 return;
161 }
162
163 being_processed = c_queue->count != 0;
164 #endif // __STEAL
165
166 c_queue->utilized = c_queue->count;
167
168 // swap copy queue ptrs
169 copy_queue * temp = *transfer_to;
170 *transfer_to = c_queue;
171 c_queue = temp;
172 unlock( mutex_lock );
173} // transfer
174
175// needed since some info needs to persist past worker lifetimes
176struct worker_info {
177 volatile unsigned long long stamp;
178 #ifdef STATS
179 size_t stolen_from;
180 #endif
181};
182static inline void ?{}( worker_info & this ) {
183 #ifdef STATS
184 this.stolen_from = 0;
185 #endif
186 this.stamp = rdtscl();
187}
188
189#ifdef STATS
190unsigned int * stolen_arr;
191unsigned int * replaced_queue;
192#endif
193thread worker {
194 work_queue ** request_queues;
195 copy_queue * current_queue;
196 executor * executor_;
197 unsigned int start, range;
198 int id;
199 #ifdef STATS
200 size_t try_steal, stolen, failed_swaps, msgs_stolen;
201 unsigned long long processed;
202 size_t gulps;
203 #endif
204};
205
206#ifdef STATS
207// aggregate counters for statistics
208size_t total_tries = 0, total_stolen = 0, total_workers, all_gulps = 0,
209 total_failed_swaps = 0, all_processed = 0, __num_actors_stats = 0, all_msgs_stolen = 0;
210#endif
211static inline void ?{}( worker & this, cluster & clu, work_queue ** request_queues, copy_queue * current_queue, executor * executor_,
212 unsigned int start, unsigned int range, int id ) {
213 ((thread &)this){ clu };
214 this.request_queues = request_queues; // array of all queues
215 this.current_queue = current_queue; // currently gulped queue (start with empty queue to use in swap later)
216 this.executor_ = executor_; // pointer to current executor
217 this.start = start; // start of worker's subrange of request_queues
218 this.range = range; // size of worker's subrange of request_queues
219 this.id = id; // worker's id and index in array of workers
220 #ifdef STATS
221 this.try_steal = 0; // attempts to steal
222 this.stolen = 0; // successful steals
223 this.processed = 0; // requests processed
224 this.gulps = 0; // number of gulps
225 this.failed_swaps = 0; // steal swap failures
226 this.msgs_stolen = 0; // number of messages stolen
227 #endif
228}
229
230static bool no_steal = false;
231struct executor {
232 cluster * cluster; // if workers execute on separate cluster
233 processor ** processors; // array of virtual processors adding parallelism for workers
234 work_queue * request_queues; // master array of work request queues
235 copy_queue * local_queues; // array of all worker local queues to avoid deletion race
236 work_queue ** worker_req_queues; // secondary array of work queues to allow for swapping
237 worker ** workers; // array of workers executing work requests
238 worker_info * w_infos; // array of info about each worker
239 unsigned int nprocessors, nworkers, nrqueues; // number of processors/threads/request queues
240 bool seperate_clus; // use same or separate cluster for executor
241}; // executor
242
243// #ifdef STATS
244// __spinlock_t out_lock;
245// #endif
246static inline void ^?{}( worker & mutex this ) with(this) {
247 #ifdef STATS
248 __atomic_add_fetch(&all_gulps, gulps,__ATOMIC_SEQ_CST);
249 __atomic_add_fetch(&all_processed, processed,__ATOMIC_SEQ_CST);
250 __atomic_add_fetch(&all_msgs_stolen, msgs_stolen,__ATOMIC_SEQ_CST);
251 __atomic_add_fetch(&total_tries, try_steal, __ATOMIC_SEQ_CST);
252 __atomic_add_fetch(&total_stolen, stolen, __ATOMIC_SEQ_CST);
253 __atomic_add_fetch(&total_failed_swaps, failed_swaps, __ATOMIC_SEQ_CST);
254
255 // per worker steal stats (uncomment alongside the lock above this routine to print)
256 // lock( out_lock __cfaabi_dbg_ctx2 );
257 // printf("Worker id: %d, processed: %llu messages, attempted %lu, stole: %lu, stolen from: %lu\n", id, processed, try_steal, stolen, __atomic_add_fetch(&executor_->w_infos[id].stolen_from, 0, __ATOMIC_SEQ_CST) );
258 // int count = 0;
259 // int count2 = 0;
260 // for ( i; range ) {
261 // if ( replaced_queue[start + i] > 0 ){
262 // count++;
263 // // printf("%d: %u, ",i, replaced_queue[i]);
264 // }
265 // if (__atomic_add_fetch(&stolen_arr[start + i],0,__ATOMIC_SEQ_CST) > 0)
266 // count2++;
267 // }
268 // printf("swapped with: %d of %u indices\n", count, executor_->nrqueues / executor_->nworkers );
269 // printf("%d of %u indices were stolen\n", count2, executor_->nrqueues / executor_->nworkers );
270 // unlock( out_lock );
271 #endif
272}
273
274static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus, size_t buf_size ) with(this) {
275 if ( nrqueues < nworkers ) abort( "nrqueues needs to be >= nworkers\n" );
276 this.nprocessors = nprocessors;
277 this.nworkers = nworkers;
278 this.nrqueues = nrqueues;
279 this.seperate_clus = seperate_clus;
280
281 if ( nworkers == nrqueues )
282 no_steal = true;
283
284 #ifdef STATS
285 stolen_arr = aalloc( nrqueues );
286 replaced_queue = aalloc( nrqueues );
287 total_workers = nworkers;
288 #endif
289
290 if ( seperate_clus ) {
291 cluster = alloc();
292 (*cluster){};
293 } else cluster = active_cluster();
294
295 request_queues = aalloc( nrqueues );
296 worker_req_queues = aalloc( nrqueues );
297 for ( i; nrqueues ) {
298 request_queues[i]{ buf_size, i };
299 worker_req_queues[i] = &request_queues[i];
300 }
301
302 processors = aalloc( nprocessors );
303 for ( i; nprocessors )
304 (*(processors[i] = alloc())){ *cluster };
305
306 local_queues = aalloc( nworkers );
307 workers = aalloc( nworkers );
308 w_infos = aalloc( nworkers );
309 unsigned int reqPerWorker = nrqueues / nworkers, extras = nrqueues % nworkers;
310
311 for ( i; nworkers ) {
312 w_infos[i]{};
313 local_queues[i]{ buf_size };
314 }
315
316 for ( unsigned int i = 0, start = 0, range; i < nworkers; i += 1, start += range ) {
317 range = reqPerWorker + ( i < extras ? 1 : 0 );
318 (*(workers[i] = alloc())){ *cluster, worker_req_queues, &local_queues[i], &this, start, range, i };
319 } // for
320}
321static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus ) { this{ nprocessors, nworkers, nrqueues, seperate_clus, __DEFAULT_EXECUTOR_BUFSIZE__ }; }
322static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues ) { this{ nprocessors, nworkers, nrqueues, __DEFAULT_EXECUTOR_SEPCLUS__ }; }
323static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers ) { this{ nprocessors, nworkers, __DEFAULT_EXECUTOR_RQUEUES__ }; }
324static inline void ?{}( executor & this, unsigned int nprocessors ) { this{ nprocessors, __DEFAULT_EXECUTOR_WORKERS__ }; }
325static inline void ?{}( executor & this ) { this{ __DEFAULT_EXECUTOR_PROCESSORS__ }; }
326
327static inline void ^?{}( executor & this ) with(this) {
328 #ifdef __STEAL
329 request sentinels[nrqueues];
330 for ( unsigned int i = 0; i < nrqueues; i++ ) {
331 insert( request_queues[i], sentinels[i] ); // force eventually termination
332 } // for
333 #else
334 request sentinels[nworkers];
335 unsigned int reqPerWorker = nrqueues / nworkers, extras = nrqueues % nworkers;
336 for ( unsigned int i = 0, step = 0, range; i < nworkers; i += 1, step += range ) {
337 range = reqPerWorker + ( i < extras ? 1 : 0 );
338 insert( request_queues[step], sentinels[i] ); // force eventually termination
339 } // for
340 #endif
341
342 for ( i; nworkers )
343 delete( workers[i] );
344
345 for ( i; nprocessors ) {
346 delete( processors[i] );
347 } // for
348
349 #ifdef STATS
350 size_t misses = 0;
351 for ( i; nrqueues ) {
352 misses += worker_req_queues[i]->missed;
353 }
354 adelete( stolen_arr );
355 adelete( replaced_queue );
356 #endif
357
358 adelete( workers );
359 adelete( w_infos );
360 adelete( local_queues );
361 adelete( request_queues );
362 adelete( worker_req_queues );
363 adelete( processors );
364 if ( seperate_clus ) delete( cluster );
365
366 #ifdef STATS
367 printf(" Actor System Stats:\n");
368 printf("\tActors Created:\t\t\t\t%lu\n\tMessages Sent:\t\t\t\t%lu\n", __num_actors_stats, all_processed);
369 printf("\tGulps:\t\t\t\t\t%lu\n\tAverage Gulp Size:\t\t\t%lu\n\tMissed gulps:\t\t\t\t%lu\n", all_gulps, all_processed / all_gulps, misses);
370 printf("\tSteal attempts:\t\t\t\t%lu\n\tSteals:\t\t\t\t\t%lu\n\tSteal failures (no candidates):\t\t%lu\n\tSteal failures (failed swaps):\t\t%lu\n",
371 total_tries, total_stolen, total_tries - total_stolen - total_failed_swaps, total_failed_swaps);
372 printf("\tMessages stolen:\t\t\t%lu\n\tAverage steal size:\t\t\t%lu\n", all_msgs_stolen, all_msgs_stolen/total_stolen);
373 #endif
374
375}
376
377// this is a static field of executor but have to forward decl for get_next_ticket
378static unsigned long int __next_ticket = 0;
379
380static inline unsigned long int __get_next_ticket( executor & this ) with(this) {
381 unsigned long int temp = __atomic_fetch_add( &__next_ticket, 1, __ATOMIC_SEQ_CST) % nrqueues;
382
383 // reserve MAX for dead actors
384 if ( temp == MAX ) temp = __atomic_fetch_add( &__next_ticket, 1, __ATOMIC_SEQ_CST) % nrqueues;
385 return temp;
386} // tickets
387
388// TODO: update globals in this file to be static fields once the static fields project is done
389static executor * __actor_executor_ = 0p;
390static bool __actor_executor_passed = false; // was an executor passed to start_actor_system
391static unsigned long int __num_actors_ = 0; // number of actor objects in system
392static struct thread$ * __actor_executor_thd = 0p; // used to wake executor after actors finish
393struct actor {
394 unsigned long int ticket; // executor-queue handle
395 Allocation allocation_; // allocation action
396};
397
398static inline void ?{}( actor & this ) {
399 // Once an actor is allocated it must be sent a message or the actor system cannot stop. Hence, its receive
400 // member must be called to end it
401 verifyf( __actor_executor_, "Creating actor before calling start_actor_system() can cause undefined behaviour.\n" );
402 this.allocation_ = Nodelete;
403 this.ticket = __get_next_ticket( *__actor_executor_ );
404 __atomic_fetch_add( &__num_actors_, 1, __ATOMIC_SEQ_CST );
405 #ifdef STATS
406 __atomic_fetch_add( &__num_actors_stats, 1, __ATOMIC_SEQ_CST );
407 #endif
408}
409static inline void ^?{}( actor & this ) {}
410
411static inline void check_actor( actor & this ) {
412 if ( this.allocation_ != Nodelete ) {
413 switch( this.allocation_ ) {
414 case Delete: delete( &this ); break;
415 case Destroy:
416 CFA_DEBUG( this.ticket = MAX; ); // mark as terminated
417 ^?{}(this);
418 break;
419 case Finished:
420 CFA_DEBUG( this.ticket = MAX; ); // mark as terminated
421 break;
422 default: ; // stop warning
423 }
424
425 if ( unlikely( __atomic_add_fetch( &__num_actors_, -1, __ATOMIC_SEQ_CST ) == 0 ) ) { // all actors have terminated
426 unpark( __actor_executor_thd );
427 }
428 }
429}
430
431struct message {
432 Allocation allocation_; // allocation action
433};
434
435static inline void ?{}( message & this ) { this.allocation_ = Nodelete; }
436static inline void ?{}( message & this, Allocation allocation ) {
437 this.allocation_ = allocation;
438 verifyf( this.allocation_ != Finished, "The Finished Allocation status is not supported for message types.\n");
439}
440static inline void ^?{}( message & this ) {
441 CFA_DEBUG( if ( this.allocation_ == Nodelete ) printf("A message at location %p was allocated but never sent.\n", &this); )
442}
443
444static inline void check_message( message & this ) {
445 switch ( this.allocation_ ) { // analyze message status
446 case Nodelete: CFA_DEBUG( this.allocation_ = Finished; ) break;
447 case Delete: delete( &this ); break;
448 case Destroy: ^?{}(this); break;
449 case Finished: break;
450 } // switch
451}
452static inline void set_allocation( message & this, Allocation state ) { this.allocation_ = state; }
453
454static inline void deliver_request( request & this ) {
455 Allocation actor_allocation = this.fn( *this.receiver, *this.msg );
456 this.receiver->allocation_ = actor_allocation;
457 check_actor( *this.receiver );
458 check_message( *this.msg );
459}
460
461// tries to atomically swap two queues and returns 0p if the swap failed
462// returns ptr to newly owned queue if swap succeeds
463static inline work_queue * try_swap_queues( worker & this, unsigned int victim_idx, unsigned int my_idx ) with(this) {
464 work_queue * my_queue = request_queues[my_idx];
465 work_queue * other_queue = request_queues[victim_idx];
466
467 // if either queue is 0p then they are in the process of being stolen
468 if ( other_queue == 0p ) return 0p;
469
470 // try to set our queue ptr to be 0p. If it fails someone moved our queue so return false
471 if ( !__atomic_compare_exchange_n( &request_queues[my_idx], &my_queue, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) )
472 return 0p;
473
474 // try to set other queue ptr to be our queue ptr. If it fails someone moved the other queue so fix up then return false
475 if ( !__atomic_compare_exchange_n( &request_queues[victim_idx], &other_queue, my_queue, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) ) {
476 /* paranoid */ verify( request_queues[my_idx] == 0p );
477 request_queues[my_idx] = my_queue; // reset my queue ptr back to appropriate val
478 return 0p;
479 }
480
481 // we have successfully swapped and since our queue is 0p no one will touch it so write back new queue ptr non atomically
482 request_queues[my_idx] = other_queue; // last write does not need to be atomic
483 return other_queue;
484}
485
486// once a worker to steal from has been chosen, choose queue to steal from
487static inline void choose_queue( worker & this, unsigned int victim_id, unsigned int swap_idx ) with(this) {
488 // have to calculate victim start and range since victim may be deleted before us in shutdown
489 const unsigned int queues_per_worker = executor_->nrqueues / executor_->nworkers;
490 const unsigned int extras = executor_->nrqueues % executor_->nworkers;
491 unsigned int vic_start, vic_range;
492 if ( extras > victim_id ) {
493 vic_range = queues_per_worker + 1;
494 vic_start = vic_range * victim_id;
495 } else {
496 vic_start = extras + victim_id * queues_per_worker;
497 vic_range = queues_per_worker;
498 }
499 unsigned int start_idx = prng( vic_range );
500
501 unsigned int tries = 0;
502 work_queue * curr_steal_queue;
503
504 for ( unsigned int i = start_idx; tries < vic_range; i = (i + 1) % vic_range ) {
505 tries++;
506 curr_steal_queue = request_queues[ i + vic_start ];
507 // avoid empty queues and queues that are being operated on
508 if ( curr_steal_queue == 0p || curr_steal_queue->being_processed || isEmpty( *curr_steal_queue->c_queue ) )
509 continue;
510
511 #ifdef STATS
512 curr_steal_queue = try_swap_queues( this, i + vic_start, swap_idx );
513 if ( curr_steal_queue ) {
514 msgs_stolen += curr_steal_queue->c_queue->count;
515 stolen++;
516 __atomic_add_fetch(&executor_->w_infos[victim_id].stolen_from, 1, __ATOMIC_RELAXED);
517 replaced_queue[swap_idx]++;
518 __atomic_add_fetch(&stolen_arr[ i + vic_start ], 1, __ATOMIC_RELAXED);
519 } else {
520 failed_swaps++;
521 }
522 #else
523 curr_steal_queue = try_swap_queues( this, i + vic_start, swap_idx );
524 #endif // STATS
525
526 return;
527 }
528
529 return;
530}
531
532// choose a worker to steal from
533static inline void steal_work( worker & this, unsigned int swap_idx ) with(this) {
534 #if RAND
535 unsigned int victim = prng( executor_->nworkers );
536 if ( victim == id ) victim = ( victim + 1 ) % executor_->nworkers;
537 choose_queue( this, victim, swap_idx );
538 #elif SEARCH
539 unsigned long long min = MAX; // smaller timestamp means longer since service
540 int min_id = 0; // use ints not uints to avoid integer underflow without hacky math
541 int n_workers = executor_->nworkers;
542 unsigned long long curr_stamp;
543 int scount = 1;
544 for ( int i = (id + 1) % n_workers; scount < n_workers; i = (i + 1) % n_workers, scount++ ) {
545 curr_stamp = executor_->w_infos[i].stamp;
546 if ( curr_stamp < min ) {
547 min = curr_stamp;
548 min_id = i;
549 }
550 }
551 choose_queue( this, min_id, swap_idx );
552 #endif
553}
554
555void main( worker & this ) with(this) {
556 #ifdef STATS
557 for ( i; executor_->nrqueues ) {
558 replaced_queue[i] = 0;
559 __atomic_store_n( &stolen_arr[i], 0, __ATOMIC_SEQ_CST );
560 }
561 #endif
562
563 // threshold of empty queues we see before we go stealing
564 const unsigned int steal_threshold = 2 * range;
565
566 // Store variable data here instead of worker struct to avoid any potential false sharing
567 unsigned int empty_count = 0;
568 request & req;
569 unsigned int curr_idx;
570 work_queue * curr_work_queue;
571
572 Exit:
573 for ( unsigned int i = 0;; i = (i + 1) % range ) { // cycle through set of request buffers
574 curr_idx = i + start;
575 curr_work_queue = request_queues[curr_idx];
576
577 // check if queue is empty before trying to gulp it
578 if ( isEmpty( *curr_work_queue->c_queue ) ) {
579 #ifdef __STEAL
580 empty_count++;
581 if ( empty_count < steal_threshold ) continue;
582 #else
583 continue;
584 #endif
585 }
586 transfer( *curr_work_queue, &current_queue );
587 #ifdef STATS
588 gulps++;
589 #endif // STATS
590 #ifdef __STEAL
591 if ( isEmpty( *current_queue ) ) {
592 if ( unlikely( no_steal ) ) continue;
593 empty_count++;
594 if ( empty_count < steal_threshold ) continue;
595 empty_count = 0;
596
597 __atomic_store_n( &executor_->w_infos[id].stamp, rdtscl(), __ATOMIC_RELAXED );
598
599 #ifdef STATS
600 try_steal++;
601 #endif // STATS
602
603 steal_work( this, start + prng( range ) );
604 continue;
605 }
606 #endif // __STEAL
607 while ( ! isEmpty( *current_queue ) ) {
608 #ifdef STATS
609 processed++;
610 #endif
611 &req = &remove( *current_queue );
612 if ( !&req ) continue;
613 if ( req.stop ) break Exit;
614 deliver_request( req );
615 }
616 #ifdef __STEAL
617 curr_work_queue->being_processed = false; // set done processing
618 empty_count = 0; // we found work so reset empty counter
619 #endif
620
621 // potentially reclaim some of the current queue's vector space if it is unused
622 reclaim( *current_queue );
623 } // for
624}
625
626static inline void send( executor & this, request & req, unsigned long int ticket ) with(this) {
627 insert( request_queues[ticket], req);
628}
629
630static inline void send( actor & this, request & req ) {
631 verifyf( this.ticket != (unsigned long int)MAX, "Attempted to send message to deleted/dead actor\n" );
632 send( *__actor_executor_, req, this.ticket );
633}
634
635static inline void start_actor_system( size_t num_thds ) {
636 __actor_executor_thd = active_thread();
637 __actor_executor_ = alloc();
638 (*__actor_executor_){ 0, num_thds, num_thds == 1 ? 1 : num_thds * 16 };
639}
640
641// TODO: potentially getting revisit number of processors
642// ( currently the value stored in active_cluster()->procs.total is often stale
643// and doesn't reflect how many procs are allocated )
644// static inline void start_actor_system() { start_actor_system( active_cluster()->procs.total ); }
645static inline void start_actor_system() { start_actor_system( 1 ); }
646
647static inline void start_actor_system( executor & this ) {
648 __actor_executor_thd = active_thread();
649 __actor_executor_ = &this;
650 __actor_executor_passed = true;
651}
652
653static inline void stop_actor_system() {
654 park( ); // will receive signal when actor system is finished
655
656 if ( !__actor_executor_passed ) delete( __actor_executor_ );
657 __actor_executor_ = 0p;
658 __actor_executor_thd = 0p;
659 __next_ticket = 0;
660 __actor_executor_passed = false;
661}
662
663// Default messages to send to any actor to change status
664// struct __DeleteMsg { inline message; } DeleteMsg;
665// void ?{}( __DeleteMsg & this ) { ((message &) this){ Finished }; }
666// struct __DestroyMsg { inline message; } DestroyMsg;
667// void ?{}( __DestroyMsg & this ) { ((message &) this){ Finished }; }
668// struct __FinishedMsg { inline message; } FinishedMsg;
669// void ?{}( __FinishedMsg & this ) { ((message &) this){ Finished }; }
670
671// Allocation receive( actor & this, __DeleteMsg & msg ) { return Delete; }
672// Allocation receive( actor & this, __DestroyMsg & msg ) { return Destroy; }
673// Allocation receive( actor & this, __FinishedMsg & msg ) { return Finished; }
Note: See TracBrowser for help on using the repository browser.