Changes between Version 5 and Version 6 of Ticket #37


Ignore:
Timestamp:
Aug 2, 2010, 1:56:42 PM (15 years ago)
Author:
becoulet
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #37 – Description

    v5 v6  
    11We sometimes use a worker-thread-like service. Each time it is recreated from scratch. Let's add an API for this.
    22
    3 Here is such a proposal
     3Here is such a proposal:
     4
     5worker.h header:
    46
    57{{{
     
    911 @this is an item pushable to a worker queue.
    1012 */
    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 
     13typedef CONTAINER_ENTRY_TYPE(CLIST) worker_entry_t;
    2714
    2815/**
    2916 @this is a worker function prototype macro.
     17 @csee worker_func_t
    3018*/
    3119#define WORKER_FUNC(n) void (n)(struct worker_thread_s *worker, \
    32                                 struct worker_item_s *item, \
     20                                worker_entry_t *entry, \
    3321                                void *priv)
    3422
     
    3927
    4028 @param worker Worker thread context, may be used to stop self
    41  @param item Item to work on
     29 @param entry Item to work on
    4230 @param priv Private data passed on init
    4331 */
    4432typedef WORKER_FUNC(worker_func_t);
    4533
    46 struct worker_thread_s
    47 {
    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 /***/
    5834void worker_thread_init(struct worker_thread_s*,
    5935                        void *stack,
     
    8359          thread already got killed, 0 if OK.
    8460 */
    85 error_t worker_thread_wakeup(struct worker_thread_s*, struct worker_item_s *item);
     61error_t worker_thread_wakeup(struct worker_thread_s*, worker_entry_t *entry);
    8662}}}
     63
     64worker.c file content
     65
     66{{{
     67#!c
     68
     69struct worker_base_s {
     70  worker_entry_t entry;
     71};
     72
     73CONTAINER_TYPE(worker_queue, CLIST, struct worker_base_s, entry);
     74CONTAINER_FUNC(...)
     75
     76struct 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
     94Usage example:
     95
     96{{{
     97#!c
     98
     99struct myjob_s
     100{
     101  ... my stuff ...
     102  worker_entry_t entry;
     103};
     104
     105CONTAINER_TYPE(myworker_queue, CLIST, struct myjob_s, entry);
     106
     107void foo()
     108{
     109  static struct myjob_s job;
     110
     111  worker_thread_wakeup(worker, &job.entry);
     112}
     113
     114static WORKER_FUNC(myfunc)
     115{
     116  struct myjob_s *job = myworker_queue_item(entry);
     117}
     118}}}