/[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 3 - (hide annotations)
Sat Oct 6 16:05:34 2007 UTC (14 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 2772 byte(s)
dynamips-0.2.6-RC2

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 dpavlin 3 pthread_mutex_t umutex = PTHREAD_MUTEX_INITIALIZER;
38     pthread_cond_t ucond = PTHREAD_COND_INITIALIZER;
39    
40 dpavlin 1 ptask_t *task;
41    
42     for(;;) {
43     PTASK_LOCK();
44     for(task=ptask_list;task;task=task->next)
45     task->cbk(task->object,task->arg);
46     PTASK_UNLOCK();
47 dpavlin 3
48     /* For testing! */
49     {
50     struct timespec t_spc;
51     m_tmcnt_t expire;
52    
53     expire = m_gettime_usec() + 2000;
54    
55     pthread_mutex_lock(&umutex);
56     t_spc.tv_sec = expire / 1000000;
57     t_spc.tv_nsec = (expire % 1000000) * 1000;
58     pthread_cond_timedwait(&ucond,&umutex,&t_spc);
59     pthread_mutex_unlock(&umutex);
60     }
61    
62     /* Old method... */
63     //usleep(ptask_sleep_time*1000);
64 dpavlin 1 }
65    
66     return NULL;
67     }
68    
69     /* Add a new task */
70     ptask_id_t ptask_add(ptask_callback cbk,void *object,void *arg)
71     {
72     ptask_t *task;
73     ptask_id_t id;
74    
75     if (!(task = malloc(sizeof(*task)))) {
76     fprintf(stderr,"ptask_add: unable to add new task.\n");
77     return(-1);
78     }
79    
80     memset(task,0,sizeof(*task));
81     task->cbk = cbk;
82     task->object = object;
83     task->arg = arg;
84    
85     PTASK_LOCK();
86     id = ++ptask_current_id;
87     assert(id != 0);
88     task->id = id;
89     task->next = ptask_list;
90     ptask_list = task;
91     PTASK_UNLOCK();
92     return(id);
93     }
94    
95     /* Remove a task */
96     int ptask_remove(ptask_id_t id)
97     {
98     ptask_t **task,*p;
99     int res = -1;
100    
101     PTASK_LOCK();
102    
103     for(task=&ptask_list;*task;task=&(*task)->next)
104     if ((*task)->id == id) {
105     p = *task;
106     *task = (*task)->next;
107     free(p);
108     res = 0;
109     break;
110     }
111    
112     PTASK_UNLOCK();
113     return(res);
114     }
115    
116     /* Initialize ptask module */
117     int ptask_init(u_int sleep_time)
118     {
119     if (sleep_time)
120     ptask_sleep_time = sleep_time;
121    
122     if (pthread_create(&ptask_thread,NULL,ptask_run,NULL) != 0) {
123     fprintf(stderr,"ptask_init: unable to create thread.\n");
124     return(-1);
125     }
126    
127     return(0);
128     }

  ViewVC Help
Powered by ViewVC 1.1.26