/[dynamips]/upstream/dynamips-0.2.6-RC2/ptask.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/dynamips-0.2.6-RC2/ptask.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Sat Oct 6 16:03:58 2007 UTC (16 years, 5 months ago) by dpavlin
Original Path: upstream/dynamips-0.2.6-RC1/ptask.c
File MIME type: text/plain
File size: 2261 byte(s)
import dynamips-0.2.6-RC1

1 dpavlin 1 /*
2     * Cisco 7200 (Predator) simulation platform.
3     * Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr)
4     *
5     * Periodic tasks centralization. Used for TX part of network devices.
6     */
7    
8     #include <stdio.h>
9     #include <stdlib.h>
10     #include <string.h>
11     #include <stdarg.h>
12     #include <unistd.h>
13     #include <errno.h>
14     #include <signal.h>
15     #include <fcntl.h>
16     #include <ctype.h>
17     #include <time.h>
18     #include <sys/time.h>
19     #include <sys/types.h>
20     #include <pthread.h>
21     #include <assert.h>
22    
23     #include "ptask.h"
24    
25     static pthread_t ptask_thread;
26     static pthread_mutex_t ptask_mutex = PTHREAD_MUTEX_INITIALIZER;
27     static ptask_t *ptask_list = NULL;
28 dpavlin 2 static u_int ptask_sleep_time = 10;
29 dpavlin 1 static ptask_id_t ptask_current_id = 0;
30    
31     #define PTASK_LOCK() pthread_mutex_lock(&ptask_mutex)
32     #define PTASK_UNLOCK() pthread_mutex_unlock(&ptask_mutex)
33    
34     /* Periodic task thread */
35     static void *ptask_run(void *arg)
36     {
37     ptask_t *task;
38    
39     for(;;) {
40     PTASK_LOCK();
41     for(task=ptask_list;task;task=task->next)
42     task->cbk(task->object,task->arg);
43     PTASK_UNLOCK();
44     usleep(ptask_sleep_time*1000);
45     }
46    
47     return NULL;
48     }
49    
50     /* Add a new task */
51     ptask_id_t ptask_add(ptask_callback cbk,void *object,void *arg)
52     {
53     ptask_t *task;
54     ptask_id_t id;
55    
56     if (!(task = malloc(sizeof(*task)))) {
57     fprintf(stderr,"ptask_add: unable to add new task.\n");
58     return(-1);
59     }
60    
61     memset(task,0,sizeof(*task));
62     task->cbk = cbk;
63     task->object = object;
64     task->arg = arg;
65    
66     PTASK_LOCK();
67     id = ++ptask_current_id;
68     assert(id != 0);
69     task->id = id;
70     task->next = ptask_list;
71     ptask_list = task;
72     PTASK_UNLOCK();
73     return(id);
74     }
75    
76     /* Remove a task */
77     int ptask_remove(ptask_id_t id)
78     {
79     ptask_t **task,*p;
80     int res = -1;
81    
82     PTASK_LOCK();
83    
84     for(task=&ptask_list;*task;task=&(*task)->next)
85     if ((*task)->id == id) {
86     p = *task;
87     *task = (*task)->next;
88     free(p);
89     res = 0;
90     break;
91     }
92    
93     PTASK_UNLOCK();
94     return(res);
95     }
96    
97     /* Initialize ptask module */
98     int ptask_init(u_int sleep_time)
99     {
100     if (sleep_time)
101     ptask_sleep_time = sleep_time;
102    
103     if (pthread_create(&ptask_thread,NULL,ptask_run,NULL) != 0) {
104     fprintf(stderr,"ptask_init: unable to create thread.\n");
105     return(-1);
106     }
107    
108     return(0);
109     }

  ViewVC Help
Powered by ViewVC 1.1.26