1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
45:
46: 47: 48: 49: 50:
51:
52: #include <basic.h>
53: #include <tk/tkernel.h>
54: #include <t2ex/util.h>
55: #include <libstr.h>
56:
57: #define USE_ATOMIC_INT ( ATOMIC_INC_USER_MODE && ATOMIC_DEC_USER_MODE && ATOMIC_BITSET_USER_MODE && ATOMIC_BITCLR_USER_MODE )
58:
59:
60:
61: 62: 63: 64:
65: EXPORT ER UMLockTmo( FastUMLock *lock, INT no, TMO tmo )
66: {
67: UINT ptn = (UINT)(1 << no);
68: UINT flg;
69: ER ercd;
70:
71: #if USE_ATOMIC_INT
72: atomic_inc(&lock->wai);
73: for ( ;; ) {
74: if ( !(atomic_bitset(&lock->flg, ptn) & ptn) ) {
75: ercd = E_OK;
76: break;
77: }
78:
79: ercd = tk_wai_flg(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
80: if ( ercd < E_OK ) {
81: break;
82: }
83: }
84: atomic_dec(&lock->wai);
85: #else
86: ercd = tk_wai_flg(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
87: #endif
88:
89: return ercd;
90: }
91: EXPORT ER UMLockTmo_u( FastUMLock *lock, INT no, TMO_U tmo )
92: {
93: UINT ptn = (UINT)(1 << no);
94: UINT flg;
95: ER ercd;
96:
97: #if USE_ATOMIC_INT
98: atomic_inc(&lock->wai);
99: for ( ;; ) {
100: if ( !(atomic_bitset(&lock->flg, ptn) & ptn) ) {
101: ercd = E_OK;
102: break;
103: }
104:
105: ercd = tk_wai_flg_u(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
106: if ( ercd < E_OK ) {
107: break;
108: }
109: }
110: atomic_dec(&lock->wai);
111: #else
112: ercd = tk_wai_flg_u(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
113: #endif
114:
115: return ercd;
116: }
117:
118: 119: 120: 121:
122: EXPORT ER UMLock( FastUMLock *lock, INT no )
123: {
124: return UMLockTmo(lock, no, TMO_FEVR);
125: }
126:
127: 128: 129: 130:
131: EXPORT ER UMUnlock( FastUMLock *lock, INT no )
132: {
133: UINT ptn = (UINT)(1 << no);
134: ER ercd;
135:
136: #if USE_ATOMIC_INT
137: atomic_bitclr(&lock->flg, ~ptn);
138: ercd = ( lock->wai == 0 )? E_OK: tk_set_flg(lock->id, ptn);
139: #else
140: ercd = tk_set_flg(lock->id, ptn);
141: #endif
142:
143: return ercd;
144: }
145:
146: 147: 148:
149: EXPORT ER CreateUMLock( FastUMLock *lock, CONST UB *name )
150: {
151: T_CFLG cflg;
152: ER ercd;
153:
154: if ( name == NULL ) {
155: cflg.exinf = NULL;
156: } else {
157: strncpy((char*)&cflg.exinf, (char*)name, sizeof(cflg.exinf));
158: }
159: cflg.flgatr = TA_TPRI | TA_WMUL | TA_NODISWAI;
160: #if USE_ATOMIC_INT
161: cflg.iflgptn = 0;
162: #else
163: cflg.iflgptn = ~0;
164: #endif
165:
166: lock->id = ercd = tk_cre_flg(&cflg);
167: if ( ercd < E_OK ) {
168: return ercd;
169: }
170:
171: lock->wai = 0;
172: lock->flg = 0;
173:
174: return E_OK;
175: }
176:
177: 178: 179:
180: EXPORT ER DeleteUMLock( FastUMLock *lock )
181: {
182: ER ercd;
183:
184: if ( lock->id <= 0 ) {
185: return E_PAR;
186: }
187:
188: ercd = tk_del_flg(lock->id);
189: if ( ercd < E_OK ) {
190: return ercd;
191: }
192:
193: lock->id = 0;
194:
195: return E_OK;
196: }