1 /* PEAK Library
2  *
3  * Copyright (c) 2003
4  *      Stephane Thiell <mbuna@bugged.org>. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 #define RCSID "$Id: task_lock.c,v 1.1.1.1 2003/12/30 02:29:31 mbuna Exp $"
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <peak/task.h>
37 
38 #include "task_private.h"
39 
40 struct __peak_task_lock
41   {
42   PEAK_STRUCT_RT_HEADER;
43   peak_task _task;               /* Owner task */
44   peak_spinlock_t _lock;
45   int _seq;
46   };
47 
48 static void __peak_task_lock_init(peak_task_lock lock, va_list vp, void *ctcx);
49 static void __peak_task_lock_finalize(peak_task_lock lock);
50 
51 PEAK_CLASS_BASE_DECLARE(task_lock);
52 
53 
54 peak_task_lock
peak_task_lock_create(peak_task task)55 peak_task_lock_create(peak_task task)
56   {
57   return PEAK_CLASS_CONSTRUCT1(task_lock, task);
58   }
59 
60 static void
__peak_task_lock_init(peak_task_lock lock,va_list vp,void * ctcx)61 __peak_task_lock_init(peak_task_lock lock, va_list vp, void *ctcx)
62   {
63   lock->_task = va_arg(vp, peak_task);
64   lock->_lock = PEAK_SPINLOCK_INITIALIZER;
65   lock->_seq = 0; /* not important */
66   }
67 
68 static void
__peak_task_lock_finalize(peak_task_lock lock)69 __peak_task_lock_finalize(peak_task_lock lock)
70   {
71   }
72 
73 void
peak_task_lock_acquire(peak_task_lock lock)74 peak_task_lock_acquire(peak_task_lock lock)
75   {
76   _peak_spinlock_lock(&lock->_lock);
77   lock->_seq++;
78   }
79 
80 int
peak_task_lock_try(peak_task_lock lock)81 peak_task_lock_try(peak_task_lock lock)
82   {
83   return _peak_spinlock_lock_try(&lock->_lock);
84   }
85 
86 void
peak_task_lock_release(peak_task_lock lock)87 peak_task_lock_release(peak_task_lock lock)
88   {
89   _peak_spinlock_unlock(&lock->_lock);
90   }
91 
92 /* /me fiddles with hand-off :p
93  */
94 void
peak_task_lock_handoff(peak_task_lock lock)95 peak_task_lock_handoff(peak_task_lock lock)
96   {
97   int seq = lock->_seq;
98 
99   if (!_peak_is_threaded)
100     PEAK_HALT; /* You should have read the nice docs ! */
101 
102   _peak_spinlock_unlock(&lock->_lock);
103 
104   /* We want the lock to be acquired!
105    * We might loose some cycles there...
106    */
107   while (lock->_seq == seq)
108     ;
109   }
110