1 /* $Id: token.c,v 1.1 2008/09/24 22:40:30 fredette Exp $ */
2 
3 /* libtme/token.c - token functions: */
4 
5 /*
6  * Copyright (c) 2008 Matt Fredette
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Matt Fredette.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <tme/common.h>
37 _TME_RCSID("$Id: token.c,v 1.1 2008/09/24 22:40:30 fredette Exp $");
38 
39 /* includes: */
40 #include <tme/token.h>
41 
42 /* this initializes a token: */
43 void
tme_token_init(struct tme_token * token)44 tme_token_init(struct tme_token *token)
45 {
46 
47   /* a constructed token is invalid: */
48   tme_memory_atomic_init_flag(&token->tme_token_invalid, TRUE);
49 
50 #if !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS)
51 
52   /* a constructed token is not busy: */
53   tme_memory_atomic_init_flag(&token->tme_token_busy, FALSE);
54 
55   /* initialize the token's invalid mutex: */
56   tme_mutex_init(&token->tme_token_invalid_mutex);
57 
58 #ifndef TME_NO_DEBUG_LOCKS
59 
60   /* initialize the file and line number of the last busier or
61      unbusier: */
62   token->_tme_token_busy_file = NULL;
63   token->_tme_token_busy_line = 0;
64 
65 #endif /* !TME_NO_DEBUG_LOCKS */
66 
67 #endif /* !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS) */
68 }
69 
70 /* this invalidates a token: */
71 void
tme_token_invalidate(struct tme_token * token)72 tme_token_invalidate(struct tme_token *token)
73 {
74 
75 #if !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS)
76 
77   /* lock the token's invalid mutex: */
78   tme_mutex_lock(&token->tme_token_invalid_mutex);
79 
80 #endif /* !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS) */
81 
82   /* invalidate the token: */
83   tme_memory_atomic_write_flag(&token->tme_token_invalid, TRUE);
84 
85 #if TME_THREADS_COOPERATIVE
86 #ifndef TME_NO_DEBUG_LOCKS
87 
88   /* this token must not be busy: */
89   assert (!tme_memory_atomic_read_flag(&token->tme_token_busy));
90 
91 #endif /* !TME_NO_DEBUG_LOCKS */
92 #else  /* !TME_THREADS_COOPERATIVE */
93 
94   /* spin while the token is busy: */
95   do { } while(tme_memory_atomic_read_flag(&token->tme_token_busy));
96 
97 #endif /* !TME_THREADS_COOPERATIVE */
98 
99 #if !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS)
100 
101   /* unlock the token's invalid mutex: */
102   tme_mutex_unlock(&token->tme_token_invalid_mutex);
103 
104 #endif /* !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS) */
105 }
106 
107 /* this clears an invalid token: */
108 #undef tme_token_invalid_clear
109 void
tme_token_invalid_clear(struct tme_token * token)110 tme_token_invalid_clear(struct tme_token *token)
111 {
112 
113 #if !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS)
114 
115   /* the token must not be busy: */
116   assert (!tme_memory_atomic_read_flag(&token->tme_token_busy));
117 
118 #endif /* !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS) */
119 
120   /* if the token is valid, return now: */
121   if (__tme_predict_false(!tme_memory_atomic_read_flag(&(token)->tme_token_invalid))) {
122     return;
123   }
124 
125 #if !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS)
126 
127   /* lock the token's invalid mutex: */
128   tme_mutex_lock(&token->tme_token_invalid_mutex);
129 
130 #endif /* !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS) */
131 
132   /* mark the token as valid again: */
133   tme_memory_atomic_write_flag(&token->tme_token_invalid, FALSE);
134 
135 #if !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS)
136 
137   /* unlock the token's invalid mutex: */
138   tme_mutex_unlock(&token->tme_token_invalid_mutex);
139 
140 #endif /* !TME_THREADS_COOPERATIVE || !defined(TME_NO_DEBUG_LOCKS) */
141 }
142