/
tls.c
796 lines (593 loc) · 17.1 KB
/
tls.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Thread local storage.
*
* By Trent Gamblin.
*
*/
/* FIXME:
*
* There are several ways to get thread local storage:
*
* 1. pthreads.
* 2. __thread keyword in gcc.
* 3. __declspec(thread) in MSVC.
* 4. TLS API under Windows.
*
* Since pthreads is available from the system everywhere except in
* Windows, this is the only case which is problematic. It appears
* that except for old mingw versions (before gcc 4.2) we can simply
* use __thread, and for MSVC we can always use __declspec(thread):
*
* However there also is a WANT_TLS configuration variable which is on
* by default and forces use of the TLS API instead. At the same time,
* the implementation using the TLS API in this file does not work
* with static linking. Someone should either completely remove
* WANT_TLS, or fix the static linking case...
*/
#include <string.h>
#include "allegro5/allegro.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_bitmap.h"
#include "allegro5/internal/aintern_display.h"
#include "allegro5/internal/aintern_file.h"
#include "allegro5/internal/aintern_fshook.h"
#include "allegro5/internal/aintern_tls.h"
#if defined(ALLEGRO_MINGW32) && !defined(ALLEGRO_CFG_DLL_TLS)
/*
* MinGW < 4.2.1 doesn't have builtin thread local storage, so we
* must use the Windows API.
*/
#if __GNUC__ < 4
#define ALLEGRO_CFG_DLL_TLS
#elif __GNUC__ == 4 && __GNUC_MINOR__ < 2
#define ALLEGRO_CFG_DLL_TLS
#elif __GNUC__ == 4 && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ < 1
#define ALLEGRO_CFG_DLL_TLS
#endif
#endif
/* Thread local storage for various per-thread state. */
typedef struct thread_local_state {
/* New display parameters */
int new_display_flags;
int new_display_refresh_rate;
int new_display_adapter;
int new_window_x;
int new_window_y;
ALLEGRO_EXTRA_DISPLAY_SETTINGS new_display_settings;
/* Current display */
ALLEGRO_DISPLAY *current_display;
/* Target bitmap */
ALLEGRO_BITMAP *target_bitmap;
/* Blender */
ALLEGRO_BLENDER current_blender;
/* Bitmap parameters */
int new_bitmap_format;
int new_bitmap_flags;
/* Files */
const ALLEGRO_FILE_INTERFACE *new_file_interface;
const ALLEGRO_FS_INTERFACE *fs_interface;
/* Error code */
int allegro_errno;
/* Destructor ownership count */
int dtor_owner_count;
} thread_local_state;
typedef struct INTERNAL_STATE {
thread_local_state tls;
ALLEGRO_BLENDER stored_blender;
ALLEGRO_TRANSFORM stored_transform;
int flags;
} INTERNAL_STATE;
ALLEGRO_STATIC_ASSERT(tls, sizeof(ALLEGRO_STATE) > sizeof(INTERNAL_STATE));
static void initialize_blender(ALLEGRO_BLENDER *b)
{
b->blend_op = ALLEGRO_ADD;
b->blend_source = ALLEGRO_ONE,
b->blend_dest = ALLEGRO_INVERSE_ALPHA;
b->blend_alpha_op = ALLEGRO_ADD;
b->blend_alpha_source = ALLEGRO_ONE;
b->blend_alpha_dest = ALLEGRO_INVERSE_ALPHA;
}
static void initialize_tls_values(thread_local_state *tls)
{
memset(tls, 0, sizeof *tls);
tls->new_display_adapter = ALLEGRO_DEFAULT_DISPLAY_ADAPTER;
tls->new_window_x = INT_MAX;
tls->new_window_y = INT_MAX;
initialize_blender(&tls->current_blender);
tls->new_bitmap_flags = ALLEGRO_VIDEO_BITMAP;
tls->new_bitmap_format = ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA;
tls->new_file_interface = &_al_file_interface_stdio;
tls->fs_interface = &_al_fs_interface_stdio;
_al_fill_display_settings(&tls->new_display_settings);
}
// FIXME: The TLS implementation below only works for dynamic linking
// right now - instead of using DllMain we should simply initialize
// on first request.
#ifdef ALLEGRO_STATICLINK
#undef ALLEGRO_CFG_DLL_TLS
#endif
#if defined(ALLEGRO_CFG_DLL_TLS)
#include "tls_dll.inc"
#elif defined(ALLEGRO_MACOSX) || defined(ALLEGRO_IPHONE)
#include "tls_pthread.inc"
#else
#include "tls_native.inc"
#endif
void _al_set_new_display_settings(ALLEGRO_EXTRA_DISPLAY_SETTINGS *settings)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
memmove(&tls->new_display_settings, settings, sizeof(ALLEGRO_EXTRA_DISPLAY_SETTINGS));
}
ALLEGRO_EXTRA_DISPLAY_SETTINGS *_al_get_new_display_settings(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return &tls->new_display_settings;
}
/* Function: al_set_new_display_flags
*/
void al_set_new_display_flags(int flags)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->new_display_flags = flags;
}
/* Function: al_get_new_display_flags
*/
int al_get_new_display_flags(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return tls->new_display_flags;
}
/* Function: al_set_new_display_refresh_rate
*/
void al_set_new_display_refresh_rate(int refresh_rate)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->new_display_refresh_rate = refresh_rate;
}
/* Function: al_get_new_display_refresh_rate
*/
int al_get_new_display_refresh_rate(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return tls->new_display_refresh_rate;
}
/* Function: al_set_new_display_adapter
*/
void al_set_new_display_adapter(int adapter)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
if (adapter < 0) {
tls->new_display_adapter = ALLEGRO_DEFAULT_DISPLAY_ADAPTER;
}
tls->new_display_adapter = adapter;
}
/* Function: al_get_new_display_adapter
*/
int al_get_new_display_adapter(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return ALLEGRO_DEFAULT_DISPLAY_ADAPTER;
return tls->new_display_adapter;
}
/* Function: al_set_new_window_position
*/
void al_set_new_window_position(int x, int y)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->new_window_x = x;
tls->new_window_y = y;
}
/* Function: al_get_new_window_position
*/
void al_get_new_window_position(int *x, int *y)
{
thread_local_state *tls;
int new_window_x = INT_MAX;
int new_window_y = INT_MAX;
if ((tls = tls_get()) != NULL) {
new_window_x = tls->new_window_x;
new_window_y = tls->new_window_y;
}
if (x)
*x = new_window_x;
if (y)
*y = new_window_y;
}
/* Make the given display current, without changing the target bitmap.
* This is used internally to change the current display transiently.
*/
bool _al_set_current_display_only(ALLEGRO_DISPLAY *display)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return false;
if (tls->current_display &&
tls->current_display->vt &&
tls->current_display->vt->unset_current_display) {
tls->current_display->vt->unset_current_display(tls->current_display);
tls->current_display = NULL;
}
if (display &&
display->vt &&
display->vt->set_current_display) {
if (!display->vt->set_current_display(display))
return false;
}
tls->current_display = display;
return true;
}
/* Function: al_get_current_display
*/
ALLEGRO_DISPLAY *al_get_current_display(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return NULL;
return tls->current_display;
}
/* Function: al_set_target_bitmap
*/
void al_set_target_bitmap(ALLEGRO_BITMAP *bitmap)
{
thread_local_state *tls;
ALLEGRO_DISPLAY *old_display;
ALLEGRO_DISPLAY *new_display;
ASSERT(!al_is_bitmap_drawing_held());
if ((tls = tls_get()) == NULL)
return;
old_display = tls->current_display;
if (bitmap == NULL) {
/* Explicitly releasing the current rendering context. */
new_display = NULL;
}
else if (bitmap->flags & ALLEGRO_MEMORY_BITMAP) {
/* Setting a memory bitmap doesn't change the rendering context. */
new_display = old_display;
}
else {
new_display = bitmap->display;
}
/* Change the rendering context if necessary. */
if (old_display != new_display) {
if (old_display &&
old_display->vt &&
old_display->vt->unset_current_display) {
old_display->vt->unset_current_display(old_display);
}
tls->current_display = new_display;
if (new_display &&
new_display->vt &&
new_display->vt->set_current_display) {
new_display->vt->set_current_display(new_display);
}
}
/* Change the target bitmap itself. */
tls->target_bitmap = bitmap;
if (bitmap &&
!(bitmap->flags & ALLEGRO_MEMORY_BITMAP) &&
new_display &&
new_display->vt &&
new_display->vt->set_target_bitmap) {
new_display->vt->set_target_bitmap(new_display, bitmap);
new_display->vt->update_transformation(new_display, bitmap);
}
}
/* Function: al_set_target_backbuffer
*/
void al_set_target_backbuffer(ALLEGRO_DISPLAY *display)
{
al_set_target_bitmap(al_get_backbuffer(display));
}
/* Function: al_get_target_bitmap
*/
ALLEGRO_BITMAP *al_get_target_bitmap(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return tls->target_bitmap;
}
/* Function: al_set_blender
*/
void al_set_blender(int op, int src, int dst)
{
al_set_separate_blender(op, src, dst, op, src, dst);
}
/* Function: al_set_separate_blender
*/
void al_set_separate_blender(int op, int src, int dst,
int alpha_op, int alpha_src, int alpha_dst)
{
thread_local_state *tls;
ALLEGRO_BLENDER *b;
ASSERT(op >= 0 && op < ALLEGRO_NUM_BLEND_OPERATIONS);
ASSERT(src >= 0 && src < ALLEGRO_NUM_BLEND_MODES);
ASSERT(dst >= 0 && src < ALLEGRO_NUM_BLEND_MODES);
ASSERT(alpha_op >= 0 && alpha_op < ALLEGRO_NUM_BLEND_OPERATIONS);
ASSERT(alpha_src >= 0 && alpha_src < ALLEGRO_NUM_BLEND_MODES);
ASSERT(alpha_dst >= 0 && alpha_dst < ALLEGRO_NUM_BLEND_MODES);
if ((tls = tls_get()) == NULL)
return;
b = &tls->current_blender;
b->blend_op = op;
b->blend_source = src;
b->blend_dest = dst;
b->blend_alpha_op = alpha_op;
b->blend_alpha_source = alpha_src;
b->blend_alpha_dest = alpha_dst;
}
/* Function: al_get_blender
*/
void al_get_blender(int *op, int *src, int *dst)
{
al_get_separate_blender(op, src, dst, NULL, NULL, NULL);
}
/* Function: al_get_separate_blender
*/
void al_get_separate_blender(int *op, int *src, int *dst,
int *alpha_op, int *alpha_src, int *alpha_dst)
{
thread_local_state *tls;
ALLEGRO_BLENDER *b;
if ((tls = tls_get()) == NULL)
return;
b = &tls->current_blender;
if (op)
*op = b->blend_op;
if (src)
*src = b->blend_source;
if (dst)
*dst = b->blend_dest;
if (alpha_op)
*alpha_op = b->blend_alpha_op;
if (alpha_src)
*alpha_src = b->blend_alpha_source;
if (alpha_dst)
*alpha_dst = b->blend_alpha_dest;
}
/* Function: al_set_new_bitmap_format
*/
void al_set_new_bitmap_format(int format)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->new_bitmap_format = format;
}
/* Function: al_set_new_bitmap_flags
*/
void al_set_new_bitmap_flags(int flags)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
/* Assume ALLEGRO_VIDEO_BITMAP if ALLEGRO_MEMORY_BITMAP is not set. */
if (!(flags & ALLEGRO_MEMORY_BITMAP))
flags |= ALLEGRO_VIDEO_BITMAP;
tls->new_bitmap_flags = flags;
}
/* Function: al_add_new_bitmap_flag
*/
void al_add_new_bitmap_flag(int flag)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->new_bitmap_flags |= flag;
}
/* Function: al_get_new_bitmap_format
*/
int al_get_new_bitmap_format(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return tls->new_bitmap_format;
}
/* Function: al_get_new_bitmap_flags
*/
int al_get_new_bitmap_flags(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return tls->new_bitmap_flags;
}
#define _STORE(x) stored->tls.x = tls->x;
/* Function: al_store_state
*/
void al_store_state(ALLEGRO_STATE *state, int flags)
{
thread_local_state *tls;
INTERNAL_STATE *stored;
if ((tls = tls_get()) == NULL)
return;
stored = (void *)state;
stored->flags = flags;
if (flags & ALLEGRO_STATE_NEW_DISPLAY_PARAMETERS) {
_STORE(new_display_flags);
_STORE(new_display_refresh_rate);
_STORE(new_display_adapter);
_STORE(new_window_x);
_STORE(new_window_y);
_STORE(new_display_settings);
}
if (flags & ALLEGRO_STATE_NEW_BITMAP_PARAMETERS) {
_STORE(new_bitmap_format);
_STORE(new_bitmap_flags);
}
if (flags & ALLEGRO_STATE_DISPLAY) {
_STORE(current_display);
}
if (flags & ALLEGRO_STATE_TARGET_BITMAP) {
_STORE(target_bitmap);
}
if (flags & ALLEGRO_STATE_BLENDER) {
stored->stored_blender = tls->current_blender;
}
if (flags & ALLEGRO_STATE_NEW_FILE_INTERFACE) {
_STORE(new_file_interface);
_STORE(fs_interface);
}
if (flags & ALLEGRO_STATE_TRANSFORM) {
ALLEGRO_BITMAP *target = al_get_target_bitmap();
if (!target)
al_identity_transform(&stored->stored_transform);
else
stored->stored_transform = target->transform;
}
}
#undef _STORE
#define _STORE(x) tls->x = stored->tls.x;
/* Function: al_restore_state
*/
void al_restore_state(ALLEGRO_STATE const *state)
{
thread_local_state *tls;
INTERNAL_STATE *stored;
int flags;
if ((tls = tls_get()) == NULL)
return;
stored = (void *)state;
flags = stored->flags;
if (flags & ALLEGRO_STATE_NEW_DISPLAY_PARAMETERS) {
_STORE(new_display_flags);
_STORE(new_display_refresh_rate);
_STORE(new_display_adapter);
_STORE(new_window_x);
_STORE(new_window_y);
_STORE(new_display_settings);
}
if (flags & ALLEGRO_STATE_NEW_BITMAP_PARAMETERS) {
_STORE(new_bitmap_format);
_STORE(new_bitmap_flags);
}
if (flags & ALLEGRO_STATE_DISPLAY) {
_STORE(current_display);
_al_set_current_display_only(tls->current_display);
}
if (flags & ALLEGRO_STATE_TARGET_BITMAP) {
_STORE(target_bitmap);
al_set_target_bitmap(tls->target_bitmap);
}
if (flags & ALLEGRO_STATE_BLENDER) {
tls->current_blender = stored->stored_blender;
}
if (flags & ALLEGRO_STATE_NEW_FILE_INTERFACE) {
_STORE(new_file_interface);
_STORE(fs_interface);
}
if (flags & ALLEGRO_STATE_TRANSFORM) {
ALLEGRO_BITMAP *bitmap = al_get_target_bitmap();
if (bitmap)
al_use_transform(&stored->stored_transform);
}
}
#undef _STORE
/* Function: al_get_new_file_interface
* FIXME: added a work-around for the situation where TLS has not yet been
* initialised when this function is called. This may happen if Allegro
* tries to load a configuration file before the system has been
* initialised. Should probably rethink the logic here...
*/
const ALLEGRO_FILE_INTERFACE *al_get_new_file_interface(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return &_al_file_interface_stdio;
/* FIXME: this situation should never arise because tls_ has the stdio
* interface set as a default, but it arises on OS X if
* pthread_getspecific() is called before pthreads_thread_init()...
*/
if (tls->new_file_interface)
return tls->new_file_interface;
else
return &_al_file_interface_stdio;
}
/* Function: al_set_new_file_interface
*/
void al_set_new_file_interface(const ALLEGRO_FILE_INTERFACE *file_interface)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->new_file_interface = file_interface;
}
/* Function: al_get_fs_interface
*/
const ALLEGRO_FS_INTERFACE *al_get_fs_interface(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return &_al_fs_interface_stdio;
if (tls->fs_interface)
return tls->fs_interface;
else
return &_al_fs_interface_stdio;
}
/* Function: al_set_fs_interface
*/
void al_set_fs_interface(const ALLEGRO_FS_INTERFACE *fs_interface)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->fs_interface = fs_interface;
}
/* Function: al_set_standard_fs_interface
*/
void al_set_standard_fs_interface(void)
{
al_set_fs_interface(&_al_fs_interface_stdio);
}
/* Function: al_get_errno
*/
int al_get_errno(void)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return 0;
return tls->allegro_errno;
}
/* Function: al_set_errno
*/
void al_set_errno(int errnum)
{
thread_local_state *tls;
if ((tls = tls_get()) == NULL)
return;
tls->allegro_errno = errnum;
}
int *_al_tls_get_dtor_owner_count(void)
{
thread_local_state *tls;
tls = tls_get();
return &tls->dtor_owner_count;
}
/* vim: set sts=3 sw=3 et: */