Changes between Version 5 and Version 6 of Ticket #37
- Timestamp:
- Aug 2, 2010, 1:56:42 PM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #37 – Description
v5 v6 1 1 We sometimes use a worker-thread-like service. Each time it is recreated from scratch. Let's add an API for this. 2 2 3 Here is such a proposal 3 Here is such a proposal: 4 5 worker.h header: 4 6 5 7 {{{ … … 9 11 @this is an item pushable to a worker queue. 10 12 */ 11 struct worker_item_s 12 { 13 CONTAINER_ENTRY_TYPE(DLIST) list_entry; 14 }; 15 16 /** @intenral */ 17 CONTAINER_TYPE(worker_internal, DLIST, struct worker_item_s, list_entry); 18 19 20 /** 21 @this permits to retrieve an actual structure from an item 22 pointer 23 */ 24 #define WORKER_ITEM_TO_STRUCT(struct_type, field, item) \ 25 (struct_type *)((uintptr_t)(item) - __builtin_offsetof(struct_type, field)) 26 13 typedef CONTAINER_ENTRY_TYPE(CLIST) worker_entry_t; 27 14 28 15 /** 29 16 @this is a worker function prototype macro. 17 @csee worker_func_t 30 18 */ 31 19 #define WORKER_FUNC(n) void (n)(struct worker_thread_s *worker, \ 32 struct worker_item_s *item, \20 worker_entry_t *entry, \ 33 21 void *priv) 34 22 … … 39 27 40 28 @param worker Worker thread context, may be used to stop self 41 @param itemItem to work on29 @param entry Item to work on 42 30 @param priv Private data passed on init 43 31 */ 44 32 typedef WORKER_FUNC(worker_func_t); 45 33 46 struct worker_thread_s47 {48 worker_internal_root_t to_handle;49 50 struct sched_context_s context;51 bool_t killed;52 lock_t lock;53 worker_func_t *func;54 void *func_priv;55 };56 57 /***/58 34 void worker_thread_init(struct worker_thread_s*, 59 35 void *stack, … … 83 59 thread already got killed, 0 if OK. 84 60 */ 85 error_t worker_thread_wakeup(struct worker_thread_s*, struct worker_item_s *item);61 error_t worker_thread_wakeup(struct worker_thread_s*, worker_entry_t *entry); 86 62 }}} 63 64 worker.c file content 65 66 {{{ 67 #!c 68 69 struct worker_base_s { 70 worker_entry_t entry; 71 }; 72 73 CONTAINER_TYPE(worker_queue, CLIST, struct worker_base_s, entry); 74 CONTAINER_FUNC(...) 75 76 struct worker_thread_s 77 { 78 worker_queue_root_t queue; 79 80 struct sched_context_s context; 81 bool_t killed; 82 lock_t lock; 83 worker_func_t *func; 84 void *func_priv; 85 }; 86 87 ... 88 { 89 worker_queue_push(&wt.queue, (worker_queue_item_t*)entry) 90 } 91 92 }}} 93 94 Usage example: 95 96 {{{ 97 #!c 98 99 struct myjob_s 100 { 101 ... my stuff ... 102 worker_entry_t entry; 103 }; 104 105 CONTAINER_TYPE(myworker_queue, CLIST, struct myjob_s, entry); 106 107 void foo() 108 { 109 static struct myjob_s job; 110 111 worker_thread_wakeup(worker, &job.entry); 112 } 113 114 static WORKER_FUNC(myfunc) 115 { 116 struct myjob_s *job = myworker_queue_item(entry); 117 } 118 }}}