Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Copyright (c) 1996 The University of Utah and |
||
3 | * the Computer Systems Laboratory at the University of Utah (CSL). |
||
4 | * All rights reserved. |
||
5 | * |
||
6 | * Permission to use, copy, modify and distribute this software is hereby |
||
7 | * granted provided that (1) source code retains these copyright, permission, |
||
8 | * and disclaimer notices, and (2) redistributions including binaries |
||
9 | * reproduce the notices in supporting documentation, and (3) all advertising |
||
10 | * materials mentioning features or use of this software display the following |
||
11 | * acknowledgement: ``This product includes software developed by the |
||
12 | * Computer Systems Laboratory at the University of Utah.'' |
||
13 | * |
||
14 | * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS |
||
15 | * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF |
||
16 | * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. |
||
17 | * |
||
18 | * CSL requests users of this software to return to csl-dist@cs.utah.edu any |
||
19 | * improvements that they make and grant CSL redistribution rights. |
||
20 | */ |
||
21 | /* |
||
22 | * Linux timer support. |
||
23 | */ |
||
24 | |||
218 | giacomo | 25 | |
2 | pj | 26 | #include<kernel/kern.h> |
218 | giacomo | 27 | #include<time.h> |
2 | pj | 28 | #include<linux/timer.h> |
29 | |||
218 | giacomo | 30 | //#define TIMER_DEBUG |
31 | |||
32 | //#define IANPATCH |
||
33 | #ifndef IANPATCH |
||
34 | |||
35 | static inline void |
||
36 | jiffies_to_timespec(unsigned long j, struct timespec *value) |
||
37 | { |
||
38 | |||
39 | value->tv_nsec = (j % HZ) * (1000000000L / HZ); |
||
40 | value->tv_sec = j / HZ; |
||
41 | |||
42 | } |
||
43 | |||
2 | pj | 44 | int add_timer(struct timer_list *timer) |
45 | { |
||
218 | giacomo | 46 | |
47 | struct timespec timeout; |
||
48 | |||
49 | jiffies_to_timespec(timer->expires, &timeout); |
||
50 | timer->event_timer = kern_event_post(&timeout, (void (*)(void *))timer->function, (void *)timer->data); |
||
51 | |||
52 | #ifdef TIMER_DEBUG |
||
53 | cprintf("Set to %ld:%ld\n",timeout.tv_sec,timeout.tv_nsec); |
||
54 | #endif |
||
55 | |||
2 | pj | 56 | return 0; |
218 | giacomo | 57 | |
2 | pj | 58 | } |
59 | |||
218 | giacomo | 60 | |
2 | pj | 61 | void del_timer(struct timer_list *timer) |
62 | { |
||
218 | giacomo | 63 | |
64 | kern_event_delete(timer->event_timer); |
||
65 | |||
2 | pj | 66 | } |
67 | |||
218 | giacomo | 68 | void mod_timer(struct timer_list *timer, unsigned long expires) |
69 | { |
||
70 | |||
71 | kern_event_delete(timer->event_timer); |
||
72 | timer->expires = expires; |
||
73 | add_timer(timer); |
||
74 | |||
75 | } |
||
76 | |||
77 | void init_timer(struct timer_list * timer) |
||
78 | { |
||
79 | } |
||
80 | |||
81 | #else |
||
82 | |||
83 | #include <pthread.h> |
||
84 | |||
85 | /*Needs to have this sort of thing in the initfile |
||
86 | * PTHREAD_register_module(1, 0, 1); |
||
87 | * |
||
88 | * TIMER_register_module(); |
||
89 | * PI_register_module(); |
||
90 | * PC_register_module(); |
||
91 | */ |
||
92 | |||
93 | pthread_attr_t task_attr; /* attributtes for the task doing the callback*/ |
||
94 | |||
95 | static inline void |
||
96 | jiffies_to_timespec(unsigned long j, struct timespec *value) |
||
97 | { |
||
98 | value->tv_nsec = (j % HZ) * (1000000000L / HZ); |
||
99 | value->tv_sec = j / HZ; |
||
100 | } |
||
101 | |||
102 | int add_timer(struct timer_list *timer) |
||
103 | { |
||
104 | int err; |
||
105 | struct sigevent ev; |
||
106 | struct sched_param task_param; |
||
107 | struct itimerspec timeout; |
||
108 | |||
109 | ev.sigev_notify = SIGEV_THREAD; |
||
110 | ev.sigev_value.sival_int = timer->data; |
||
111 | ev.sigev_notify_function = timer->function; |
||
112 | ev.sigev_notify_attributes = &task_attr; |
||
113 | |||
114 | pthread_attr_init(&task_attr); |
||
115 | pthread_attr_setdetachstate(&task_attr, PTHREAD_CREATE_DETACHED); |
||
116 | pthread_attr_setschedpolicy(&task_attr, SCHED_RR); |
||
117 | task_param.sched_priority = 10; |
||
118 | pthread_attr_setschedparam(&task_attr, &task_param); |
||
119 | |||
120 | err =timer_create(CLOCK_REALTIME, &ev, &(timer->sharktimer)); |
||
121 | if (err !=0 ) { cprintf("timer: unable to create timer\n"); } |
||
122 | |||
123 | timeout.it_interval.tv_sec=0; |
||
124 | timeout.it_interval.tv_nsec=0; |
||
125 | jiffies_to_timespec(timer->expires, &(timeout.it_value)); |
||
126 | |||
127 | err = timer_settime(timer->sharktimer, 0, &timeout, NULL); |
||
128 | if (err !=0 ) { cprintf("timer: unable to timer_settime\n"); } |
||
129 | |||
130 | cprintf("Set to %ld:%ld\n",timeout.it_value.tv_sec,timeout.it_value.tv_nsec); |
||
131 | return err; |
||
132 | } |
||
133 | |||
134 | |||
135 | void del_timer(struct timer_list *timer) |
||
136 | { |
||
137 | timer_delete(timer->sharktimer); |
||
138 | } |
||
139 | |||
140 | void mod_timer(struct timer_list *timer, unsigned long expires) |
||
141 | { |
||
142 | timer_delete(timer->sharktimer); |
||
143 | timer->expires=expires; |
||
144 | add_timer(timer); |
||
145 | } |
||
146 | |||
147 | void init_timer(struct timer_list * timer) |
||
148 | { |
||
149 | } |
||
150 | |||
151 | #endif |