7 /* FIXME Remove the "0 &&" below to make the test pass.
8 * This should not be a Lightning issue. It is crashing
10 * Thread 2 "catomic" received signal SIGSEGV, Segmentation fault.
11 * [Switching to Thread 0xf6acc480 (LWP 2561)]
12 * _IO_acquire_lock_fct (p=<synthetic pointer>) at libioP.h:797
13 * 797 libioP.h: No such file or directory.
15 * #0 _IO_acquire_lock_fct (p=<synthetic pointer>) at libioP.h:797
16 * #1 _IO_puts (str=0x1 <error: Cannot access memory at address 0x1>)
18 * #2 0xf8eca180 in ?? ()
19 * unless using these hacks, to not use stdio.
20 * Note that the problem will still happen on purpose, to not risk hiding
23 #if 0 && defined(__hppa__)
29 jit_pushargi(STDOUT_FILENO); \
30 jit_pushargi((jit_word_t)"ok\n"); \
39 jit_pushargi((jit_word_t)"ok"); \
51 int debug_offset(void)
54 pthread_t self = pthread_self();
55 for (i = 0; i < 4; ++i)
63 printf(" spin %d : %ld\n", debug_offset(), lock);
68 printf(" lock %d : %ld\n", debug_offset(), lock);
71 void debug_unlock(void)
73 printf("unlock %d : %ld\n", debug_offset(), lock);
75 #define DEBUG_SPIN() jit_calli(debug_spin)
76 #define DEBUG_LOCK() jit_calli(debug_lock)
77 #define DEBUG_UNLOCK() jit_calli(debug_unlock)
79 #define DEBUG_SPIN() /**/
80 #define DEBUG_LOCK() /**/
81 #define DEBUG_UNLOCK() /**/
84 void alarm_handler(int unused)
90 main(int argc, char *argv[])
94 jit_node_t *jmpi_main, *label;
95 jit_node_t *func0, *func1, *func2, *func3;
96 jit_node_t *patch0, *patch1, *patch2, *patch3;
98 /* If there is any bug, do not hang in "make check" */
99 signal(SIGALRM, alarm_handler);
103 _jit = jit_new_state();
105 jmpi_main = jit_jmpi();
107 #define defun(name, line) \
109 jit_note("catomic.c", line); \
110 name = jit_label(); \
112 jit_movi(JIT_V0, (jit_word_t)&lock); \
113 jit_movi(JIT_V1, 0); \
114 jit_movi(JIT_V2, line); \
115 /* spin until get the lock */ \
117 label = jit_label(); \
118 jit_casr(JIT_R0, JIT_V0, JIT_V1, JIT_V2); \
119 jit_patch_at(jit_beqi(JIT_R0, 0), label); \
120 /* lock acquired */ \
123 /* pretend to be doing something useful for 0.01 sec
124 * while holding the lock */ \
125 jit_pushargi(10000); \
126 jit_finishi(usleep); \
129 jit_movi(JIT_V1, 0); \
130 jit_str(JIT_V0, JIT_V1); \
131 /* Now test casi */ \
132 jit_movi(JIT_V1, 0); \
133 jit_movi(JIT_V2, line); \
134 /* spin until get the lock */ \
136 label = jit_label(); \
137 jit_casi(JIT_R0, (jit_word_t)&lock, JIT_V1, JIT_V2); \
138 jit_patch_at(jit_beqi(JIT_R0, 0), label); \
139 /* lock acquired */ \
142 /* pretend to be doing something useful for 0.01 sec
143 * while holding the lock */ \
144 jit_pushargi(10000); \
145 jit_finishi(usleep); \
149 jit_movi(JIT_V1, 0); \
150 jit_str(JIT_V0, JIT_V1); \
153 defun(func0, __LINE__);
154 defun(func1, __LINE__);
155 defun(func2, __LINE__);
156 defun(func3, __LINE__);
158 jit_patch(jmpi_main);
160 jit_note("catomic.c", __LINE__);
164 /* set JIT_R0 to thread function */ \
165 jit_patch_at(jit_movi(JIT_R0, 0), func##tid); \
167 /* pthread_t first argument */ \
168 jit_pushargi((jit_word_t)(tids + tid)); \
169 /* pthread_attr_t second argument */ \
170 jit_pushargi((jit_word_t)NULL); \
171 /* start routine third argument */ \
172 jit_pushargr(JIT_R0); \
173 /* argument to start routine fourth argument */ \
174 jit_pushargi((jit_word_t)NULL); \
176 jit_finishi(pthread_create);
177 /* spawn four threads */
184 /* load pthread_t value in JIT_R0 */ \
185 jit_movi(JIT_R0, (jit_word_t)tids); \
186 if (__WORDSIZE == 64 && sizeof(pthread_t) == 4) \
187 jit_ldxi_i(JIT_R0, JIT_R0, tid * sizeof(pthread_t)); \
189 jit_ldxi(JIT_R0, JIT_R0, tid * sizeof(pthread_t)); \
191 jit_pushargr(JIT_R0); \
192 jit_pushargi((jit_word_t)NULL); \
193 jit_finishi(pthread_join);
194 /* wait for threads to finish */
213 /* let first thread acquire the lock */