1- #include <linux/kernel.h>
21#include <linux/module.h>
32#include <linux/init.h>
43#include <linux/hashtable.h>
1817#include <linux/kthread.h>
1918#include <linux/ktime.h>
2019#include <linux/sched.h>
20+ #include <linux/kernel.h>
2121#include "eosi_barrier_kernel.h"
2222
2323static struct file_operations eosi_barrier_fops = {
@@ -39,7 +39,7 @@ static struct miscdevice eosi_barrier_B = {
3939 .fops = & eosi_barrier_fops ,
4040};
4141
42- static LIST_HEAD (barrier_list );
42+ static LIST_HEAD (tgid_list );
4343
4444
4545struct barrier_struct * get_curr_barrier (struct inode * node ) {
@@ -57,7 +57,8 @@ struct barrier_struct* get_curr_barrier(struct inode* node) {
5757
5858//init
5959static int eosi_barrier_open (struct inode * node , struct file * file ) {
60- printk (KERN_ALERT "eosi_barrier: Open\n" );
60+ printk (KERN_ALERT "eosi_barrier2: Open\n" );
61+ printk (KERN_ALERT "eosi_barrier: sizeof(unsigned long):%d sizeof(int):%d\n" , sizeof (unsigned long long ), sizeof (int ));
6162 printk (KERN_ALERT "eosi_barrier: Open Done\n" );
6263 return 0 ;
6364}
@@ -68,16 +69,141 @@ static int eosi_barrier_release(struct inode* node, struct file* file) {
6869 printk (KERN_ALERT "eosi_barrier: Release Done\n" ); return 0 ;
6970}
7071
72+ barrier_struct * barray [10 ];
73+ static unsigned int basic_bid = 0 ;
74+ int barrier_init (barrier_info * binfo ) {
75+ barrier_struct * barrier = NULL ;
76+
77+ printk (KERN_ALERT "barrier_init\n" );
78+
79+ if (0 > binfo -> count ) return -1 ;
80+ //search tgid linkedlist
81+ //if the tgid block does not exist, then allocate the new one tgid block.
82+
83+ binfo -> barrier_id = basic_bid ;
84+ basic_bid ++ ;
85+ barrier = kmalloc (sizeof (barrier_struct ), GFP_KERNEL );
86+ init_barrier_struct (barrier , binfo -> count , binfo -> barrier_id );
87+
88+ //link the corresponding barrier block.
89+ barray [basic_bid - 1 ] = barrier ;
90+
91+ printk (KERN_ALERT "barrier_init done\n" );
92+ return 0 ;
93+ }
94+
95+ void printSYNC_start (char * fun , barrier_struct * barrier ) {
96+ printk (KERN_ALERT "%s: tgid:%d, tid:%d, id:%d, curr(tgid, tid):(%d,%d)\n" , fun , barrier -> tgid , barrier -> tid , barrier -> id , task_tgid_vnr (current ), task_pid_vnr (current ));
97+ }
98+
99+ void printSYNC_done (char * fun , barrier_struct * barrier ) {
100+ char s [30 ];
101+ snprintf (s , 30 , "%s Done" , fun );
102+
103+ printk (KERN_ALERT "%s: tgid:%d, tid:%d, id:%d, curr (tgid, tid):(%d, %d)\n" , s , barrier -> tgid , barrier -> tid , barrier -> id , task_tgid_vnr (current ), task_pid_vnr (current ));
104+
105+ }
106+ void wake_up_here (barrier_struct * barrier ) {
107+ char * s = "wake_up_here" ;
108+
109+ printSYNC_start (s , barrier );
110+ wake_up_interruptible_all (& (barrier -> waitQ ));
111+ printSYNC_done (s , barrier );
112+
113+ }
114+
115+ void wait_here (barrier_struct * barrier ) {
116+ char * s = "wait_here" ;
117+ DEFINE_WAIT (wait );
118+
119+ printSYNC_start (s , barrier );
120+
121+ prepare_to_wait (& (barrier -> waitQ ), & wait , TASK_INTERRUPTIBLE );
122+ schedule ();
123+ finish_wait (& (barrier -> waitQ ), & wait );
124+ printSYNC_done (s , barrier );
125+
126+ }
127+
128+ int barrier_wait (unsigned int barrier_id ) {
129+ barrier_struct * barrier = barray [barrier_id ];
130+ char * s = "barrier_wait" ;
131+
132+ printSYNC_start (s , barrier );
133+
134+ spin_lock (& (barrier -> lock_total ));
135+
136+ while (barrier -> total > BARRIER_FLAG ) {
137+ spin_unlock (& (barrier -> lock_total ));
138+ wait_here (barrier );
139+ spin_lock (& (barrier -> lock_total ));
140+ }
141+
142+ if (BARRIER_FLAG == barrier -> total ) barrier -> total = 0 ;
143+
144+ barrier -> total ++ ;
145+
146+ if (barrier -> total == barrier -> count ) {
147+ barrier -> total += BARRIER_FLAG - 1 ;
148+ wake_up_here (barrier );
149+ //spin_unlock(&(barrier->lock_total));
150+
151+ } else { //sleep here;
152+ while (barrier -> total < BARRIER_FLAG ) {
153+ spin_unlock (& (barrier -> lock_total ));
154+ wait_here (barrier );
155+ spin_lock (& (barrier -> lock_total ));
156+ }
157+
158+ barrier -> total -- ;
159+
160+ if (barrier -> total == BARRIER_FLAG ) {
161+ wake_up_here (barrier );
162+ }
163+
164+ // spin_unlock(&(barrier->lock_total));
165+ }
166+
167+ spin_unlock (& (barrier -> lock_total ));
168+ printSYNC_done (s , barrier );
169+ return 0 ;
170+ }
171+
172+ int barrier_destroy (unsigned int barrier_id ) {
173+ char * s = "barrier_destroy" ;
174+ barrier_struct * barrier = barray [barrier_id ];
175+
176+ printSYNC_start (s , barrier );
177+
178+ spin_lock (& (barrier -> lock_total ));
179+
180+ while (barrier -> total > BARRIER_FLAG ) {
181+ spin_unlock (& (barrier -> lock_total ));
182+ wait_here (barrier );
183+ spin_lock (& (barrier -> lock_total ));
184+ }
185+
186+ spin_unlock (& (barrier -> lock_total ));
187+
188+ //Recycle HERE//
189+
190+ printSYNC_done (s , barrier );
191+ return 0 ;
192+ }
193+
71194static long eosi_barrier_ioctl (struct file * file , unsigned int cmd , unsigned long arg ) {
72- int ret ;
195+ int ret = 0 ;
73196
74197 printk (KERN_ALERT "ioctl\n" );
75- switch (arg ) {
198+ switch (cmd ) {
76199 case BARRIER_INIT :
200+ ret = barrier_init ((barrier_info * )arg );
77201 break ;
78202 case BARRIER_WAIT :
203+ ret = barrier_wait ((unsigned int )arg );
79204 break ;
80205 case BARRIER_DESTROY :
206+ ret = barrier_destroy ((unsigned int )arg );
81207 break ;
82208 default :
83209 return - EINVAL ;
@@ -95,24 +221,38 @@ static int reg_misc(struct miscdevice* md) {
95221
96222 return error ;
97223}
224+
98225static void dereg_misc (struct miscdevice * md ) {
99226 misc_deregister (md );
100227}
101228
102- static void init_barrier_struct (barrier_struct * barrier , int count , int * id ) {
229+ static void init_barrier_struct (barrier_struct * barrier , int count , unsigned int id ) {
230+ barrier -> count = count ;
231+ barrier -> total = BARRIER_FLAG ;
232+ barrier -> id = id ;
233+ barrier -> tgid = task_tgid_vnr (current );
234+ barrier -> tid = task_pid_vnr (current );
235+ spin_lock_init (& (barrier -> lock_total ));
236+ init_waitqueue_head (& (barrier -> waitQ ));
237+ }
238+
239+ static void init_per_tgid (per_tgid * each_tgid ) {
240+ each_tgid -> tgid = task_tgid_vnr (current );
103241}
242+ #ifndef _BARRIER_MGMT_
243+ #endif
104244
105245static int __init eosi_barrier_init (void ) {
106246 /*!!!!!*/
107247 //CHECK!!! INIT
108- printk (KERN_ALERT "hc_sr04 : INIT\n" );
248+ printk (KERN_ALERT "eosi_barrier : INIT\n" );
109249 if ( reg_misc (& eosi_barrier_A ) || reg_misc (& eosi_barrier_B ) )
110250 return 0 ;
111251
112- // init_hcsr_struct(&hcA, &hc_sr04_A , A_pins, A_pin_str);
113- // init_hcsr_struct(&hcB, &hc_sr04_B , B_pins, B_pin_str);
252+ // init_hcsr_struct(&hcA, &eosi_barrier_A , A_pins, A_pin_str);
253+ // init_hcsr_struct(&hcB, &eosi_barrier_B , B_pins, B_pin_str);
114254
115- printk (KERN_ALERT "hc_sr04 : INIT DONE\n" );
255+ printk (KERN_ALERT "eosi_barrier : INIT DONE\n" );
116256 return 0 ;
117257}
118258
0 commit comments