Source

pypy / pypy / translator / stm / src_stm / et.c

Full commit
  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
/* -*- c-basic-offset: 2 -*- */

/* XXX assumes that time never wraps around (in a 'long'), which may be
 * correct on 64-bit machines but not on 32-bit machines if the process
 * runs for long enough.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>

#ifndef RPY_STM
/* for tests, a few custom defines */
#define RPY_STM_DEBUG_PRINT     1
#define PYPY_DEBUG_START(s)     fprintf(stderr, "start: %s\n", s)
#define PYPY_DEBUG_STOP(s)      fprintf(stderr, " stop: %s\n", s)
#define PYPY_HAVE_DEBUG_PRINTS  1
#define PYPY_DEBUG_FILE         stderr
#endif

#include "et.h"

/************************************************************/

typedef Unsigned revision_t;
#define INEVITABLE  ((revision_t)-1)
#define LOCKED  ((revision_t)-0x10000)

#include "atomic_ops.h"
#include "lists.c"

/************************************************************/

#define ABORT_REASONS 5
#define SPINLOOP_REASONS 3

struct tx_descriptor {
  jmp_buf *setjmp_buf;
  revision_t start_time;
  revision_t my_lock;
  long atomic;   /* 0 = not atomic, > 0 atomic */
  long reads_size_limit, reads_size_limit_nonatomic; /* see should_break_tr. */
  int active;    /* 0 = inactive, 1 = regular, 2 = inevitable */
  int readonly_updates;
  unsigned int num_commits;
  unsigned int num_aborts[ABORT_REASONS];
  unsigned int num_spinloops[SPINLOOP_REASONS];
  struct GcPtrList list_of_read_objects;
  struct GcPtrList gcroots;
  struct G2L global_to_local;
  struct FXCache recent_reads_cache;
};

static volatile revision_t global_cur_time = 2;              /* always even */
static volatile revision_t next_locked_value = LOCKED + 3;   /* always odd */
static __thread struct tx_descriptor *thread_descriptor = NULL;

/************************************************************/

static void ValidateDuringTransaction(struct tx_descriptor *);
static void CancelLocks(struct tx_descriptor *d);
static void AbortTransaction(int num);
static void SpinLoop(int num);
static gcptr Localize(struct tx_descriptor *d, gcptr R);

static _Bool is_inevitable(struct tx_descriptor *d)
{
  /* Assert that we are running a transaction.
   *      Returns True if this transaction is inevitable. */
  assert(d->active == 1 + !d->setjmp_buf);
  return d->active == 2;
}

static pthread_mutex_t mutex_inevitable = PTHREAD_MUTEX_INITIALIZER;
#define inev_mutex_acquire()  pthread_mutex_lock(&mutex_inevitable)
#define inev_mutex_release()  pthread_mutex_unlock(&mutex_inevitable)

/************************************************************/

static inline void PossiblyUpdateChain(
        struct tx_descriptor *d,
        gcptr G, gcptr R, gcptr R_Container, size_t offset)
{
  if (R != G && --d->readonly_updates < 0)
    {
      d->readonly_updates = 148;   /* XXX tweak */
      // compress the chain
      while ((gcptr)G->h_revision != R)
        {
          gcptr G_next = (gcptr)G->h_revision;
          G->h_revision = (revision_t)R;
          G = G_next;
        }
      // update the original field
      if (R_Container != NULL)
        {
          gcptr *ref = (gcptr *)(((char *)R_Container) + offset);
          *ref = R;
        }
    }
}

static gcptr LatestGlobalRevision(struct tx_descriptor *d, gcptr G,
                                  gcptr R_Container, size_t offset)
{
  gcptr R = G;
  revision_t v;
 retry:
  while (!((v = R->h_revision) & 1))   // "is a pointer", i.e.
    {                                  //   "has a more recent revision"
      R = (gcptr)v;
    }
  if (__builtin_expect(v > d->start_time, 0))   // object too recent?
    {
      if (v >= LOCKED)
        {
          SpinLoop(1);     // spinloop until it is no longer LOCKED
          goto retry;
        }
      ValidateDuringTransaction(d);    // try to move start_time forward
      goto retry;                      // restart searching from R
    }
  PossiblyUpdateChain(d, G, R, R_Container, offset);
  return R;
}

static inline gcptr AddInReadSet(struct tx_descriptor *d, gcptr R)
{
  switch (fxcache_add(&d->recent_reads_cache, R)) {

  case 0:
      /* not in the cache: it may be the first time we see it,
       * so insert it into the list */
      gcptrlist_insert(&d->list_of_read_objects, R);
      break;

  case 2:
      /* already in the cache, and FX_THRESHOLD reached */
      return Localize(d, R);
  }
  return R;
}

static inline gcptr _direct_read_barrier(gcptr G, gcptr R_Container,
                                         size_t offset)
{
  gcptr R;
  struct tx_descriptor *d = thread_descriptor;
  assert(d->active);
  if (!(G->h_tid & GCFLAG_POSSIBLY_OUTDATED))
    {
      R = G;
    }
  else
    {
      R = LatestGlobalRevision(d, G, R_Container, offset);
      if (R->h_tid & GCFLAG_POSSIBLY_OUTDATED)
        {
          wlog_t *entry;
          gcptr L;
          G2L_FIND(d->global_to_local, R, entry, goto not_found);
          L = entry->val;
          assert(L->h_revision == (revision_t)R);
          if (R_Container && !(R_Container->h_tid & GCFLAG_GLOBAL))
            {    /* R_Container is a local object */
              gcptr *ref = (gcptr *)(((char *)R_Container) + offset);
              *ref = L;   /* fix in-place */
            }
          return L;

        not_found:;
        }
    }
  R = AddInReadSet(d, R);
  return R;
}

gcptr stm_DirectReadBarrier(gcptr G)
{
  return _direct_read_barrier(G, NULL, 0);
}

gcptr stm_DirectReadBarrierFromR(gcptr G, gcptr R_Container, size_t offset)
{
  return _direct_read_barrier(G, R_Container, offset);
}

gcptr stm_RepeatReadBarrier(gcptr O)
{
  // LatestGlobalRevision(O) would either return O or abort
  // the whole transaction, so omitting it is not wrong
  struct tx_descriptor *d = thread_descriptor;
  gcptr L;
  wlog_t *entry;
  G2L_FIND(d->global_to_local, O, entry, return O);
  L = entry->val;
  assert(L->h_revision == (revision_t)O);
  return L;
}

#if 0
gcptr _NonTransactionalReadBarrier(gcptr P)
{
  /* testing only: use this outside transactions to check the state */
  revision_t v;
  struct tx_descriptor *d = thread_descriptor;
  assert(d == NULL || !d->active);

  assert(P->h_tid & GCFLAG_GLOBAL);

  while (!((v = P->h_revision) & 1))   // "is a pointer", i.e.
    {                                  //   "has a more recent revision"
      assert(P->h_tid & GCFLAG_POSSIBLY_OUTDATED);
      fprintf(stderr, "[%p->%p]\n", P, (gcptr)v);
      P = (gcptr)v;
    }
  if (P->h_tid & GCFLAG_POSSIBLY_OUTDATED)
    fprintf(stderr, "[---%p possibly outdated---]\n", P);
  return P;
}
#endif

static gcptr Localize(struct tx_descriptor *d, gcptr R)
{
  wlog_t *entry;
  gcptr L;
  G2L_FIND(d->global_to_local, R, entry, goto not_found);
  L = entry->val;
  assert(L->h_revision == (revision_t)R);
  return L;

 not_found:
  L = pypy_g__stm_duplicate(R);
  assert(!(L->h_tid & GCFLAG_GLOBAL));   /* must be removed by stm_duplicate */
  assert(!(L->h_tid & GCFLAG_POSSIBLY_OUTDATED));    /* must be removed by ^ */
  assert(L->h_tid & GCFLAG_LOCAL_COPY);    /* must be added by stm_duplicate */
  assert(L->h_tid & GCFLAG_NOT_WRITTEN); /* must not be set in the 1st place */
  L->h_revision = (revision_t)R;     /* back-reference to the original */
  g2l_insert(&d->global_to_local, R, L);
  return L;
}

gcptr stm_WriteBarrier(gcptr P)
{
  gcptr R, W;
  if (!(P->h_tid & GCFLAG_GLOBAL))
    {
      W = P;
      R = (gcptr)W->h_revision;
    }
  else
    {
      struct tx_descriptor *d = thread_descriptor;
      assert(d->active);
      if (P->h_tid & GCFLAG_POSSIBLY_OUTDATED)
        R = LatestGlobalRevision(d, P, NULL, 0);
      else
        R = P;
      W = Localize(d, R);
    }
  W->h_tid &= ~GCFLAG_NOT_WRITTEN;
  R->h_tid |= GCFLAG_POSSIBLY_OUTDATED;
  return W;
}

gcptr stm_WriteBarrierFromReady(gcptr R)
{
  gcptr W;
  if (!(R->h_tid & GCFLAG_GLOBAL))
    {
      W = R;
      R = (gcptr)W->h_revision;
    }
  else
    {
      struct tx_descriptor *d = thread_descriptor;
      assert(d->active);
      W = Localize(d, R);
    }
  W->h_tid &= ~GCFLAG_NOT_WRITTEN;
  R->h_tid |= GCFLAG_POSSIBLY_OUTDATED;
  return W;
}

/************************************************************/

static revision_t GetGlobalCurTime(struct tx_descriptor *d)
{
  revision_t t;
  assert(!is_inevitable(d));    // must not be myself inevitable
  while (1)
    {
      t = global_cur_time;
      if (t != INEVITABLE)
        return t;
      // there is another transaction that is inevitable
      inev_mutex_acquire();     // wait until released
      inev_mutex_release();
      // retry
    }
}

static void ValidateDuringTransaction(struct tx_descriptor *d)
{

  long i, size = d->list_of_read_objects.size;
  gcptr *items = d->list_of_read_objects.items;

  assert(!is_inevitable(d));
  d->start_time = GetGlobalCurTime(d);   // copy from the global time

  for (i=0; i<size; i++)
    {
      gcptr R = items[i];
      revision_t v;
    retry:
      v = R->h_revision;
      if (!(v & 1))               // "is a pointer", i.e.
        AbortTransaction(1);      //   "has a more recent revision"
      if (v >= LOCKED)            // locked
        goto retry;
    }
}

static _Bool ValidateDuringCommit(struct tx_descriptor *d)
{
  long i, size = d->list_of_read_objects.size;
  gcptr *items = d->list_of_read_objects.items;
  revision_t my_lock = d->my_lock;

  for (i=0; i<size; i++)
    {
      gcptr R = items[i];
      revision_t v = R->h_revision;
      if (!(v & 1))               // "is a pointer", i.e.
        return 0;                 //   "has a more recent revision"
      if (v >= LOCKED)            // locked
        if (v != my_lock)         // and not by me
          return 0;               // XXX abort or spinloop??
    }
  return 1;
}

/************************************************************/

static void SpinLoop(int num)
{
  struct tx_descriptor *d = thread_descriptor;
  assert(d->active);
  assert(num < SPINLOOP_REASONS);
  d->num_spinloops[num]++;
  spinloop();
}

static void AbortTransaction(int num)
{
  struct tx_descriptor *d = thread_descriptor;
  assert(d->active);
  assert(!is_inevitable(d));
  assert(num < ABORT_REASONS);
  d->num_aborts[num]++;

  CancelLocks(d);

  gcptrlist_clear(&d->list_of_read_objects);
  gcptrlist_clear(&d->gcroots);
  g2l_clear(&d->global_to_local);
  fxcache_clear(&d->recent_reads_cache);

#ifdef RPY_STM_DEBUG_PRINT
  PYPY_DEBUG_START("stm-abort");
  if (PYPY_HAVE_DEBUG_PRINTS)
      fprintf(PYPY_DEBUG_FILE, "thread %lx aborting %d\n",
                               (long)d->my_lock, num);
  PYPY_DEBUG_STOP("stm-abort");
#endif

  // notifies the CPU that we're potentially in a spin loop
  SpinLoop(0);
  // jump back to the setjmp_buf (this call does not return)
  d->active = 0;
  longjmp(*d->setjmp_buf, 1);
}

/************************************************************/

static void update_reads_size_limit(struct tx_descriptor *d)
{
  /* 'reads_size_limit' is set to LONG_MAX if we are atomic; else
     we copy the value from reads_size_limit_nonatomic. */
  d->reads_size_limit = d->atomic ? LONG_MAX : d->reads_size_limit_nonatomic;
}

static void init_transaction(struct tx_descriptor *d)
{
  assert(d->active == 0);
  assert(d->list_of_read_objects.size == 0);
  assert(d->gcroots.size == 0);
  assert(!g2l_any_entry(&d->global_to_local));
  assert(fxcache_is_clear(&d->recent_reads_cache));
}

void BeginTransaction(jmp_buf* buf)
{
  struct tx_descriptor *d = thread_descriptor;
  init_transaction(d);
  d->active = 1;
  d->setjmp_buf = buf;
  d->start_time = GetGlobalCurTime(d);
  update_reads_size_limit(d);
}

static void AcquireLocks(struct tx_descriptor *d)
{
  revision_t my_lock = d->my_lock;
  gcptr *item = d->gcroots.items;
  // gcroots should be sorted in some deterministic order by construction

  while (item[0] != NULL)
    {
      gcptr R = (gcptr)item[0]->h_revision;
      revision_t v;
    retry:
      v = R->h_revision;
      if (!(v & 1))            // "is a pointer", i.e.
        AbortTransaction(0);   //   "has a more recent revision"
      if (v >= LOCKED)         // already locked by someone else
        {
          // we can always spinloop here: deadlocks should be impossible,
          // because FindRootsForLocalCollect's G2L_LOOP_FORWARD should
          // ensure a consistent ordering of the R's.
          SpinLoop(2);
          goto retry;
        }
      if (!bool_cas((volatile revision_t *)&R->h_revision, v, my_lock))
        goto retry;

      item[1] = (gcptr)v;
      item += 2;
    }
}

static void CancelLocks(struct tx_descriptor *d)
{
  long i, lastitem = d->gcroots.size - 2;
  gcptr *items = d->gcroots.items;
  for (i=0; i<=lastitem; i+=2)
    {
      gcptr R = items[i]->h_revision;
      revision_t v = (revision_t)items[i+1];
      if (v == 0)
        break;
      R->h_revision = v;
      // if we're going to retry later, and abort,
      // then we must not re-cancel the same entries
      items[i+1] = (gcptr)0;
    }
}

static void UpdateChainHeads(struct tx_descriptor *d, revision_t cur_time)
{
  gcptr *item, *itemstart = d->gcroots.items;
  revision_t new_revision = cur_time + 1;     // make an odd number
  assert(new_revision & 1);

  for (item = itemstart; item[0] != NULL; item += 2)
    {
      gcptr L = item[0];
      assert((L->h_tid & (GCFLAG_GLOBAL |
                          GCFLAG_NOT_WRITTEN |
                          GCFLAG_POSSIBLY_OUTDATED)) ==
              (GCFLAG_GLOBAL | GCFLAG_NOT_WRITTEN));
      item[1] = (gcptr)L->h_revision;    /* old value: pointer to R */
      L->h_revision = new_revision;
    }
  smp_wmb();
  for (item = itemstart; item[0] != NULL; item += 2)
    {
      gcptr L = item[0];
      gcptr R = item[1];
      assert((R->h_tid & (GCFLAG_GLOBAL |
                          GCFLAG_NOT_WRITTEN |
                          GCFLAG_POSSIBLY_OUTDATED)) ==
              (GCFLAG_GLOBAL | GCFLAG_NOT_WRITTEN | GCFLAG_POSSIBLY_OUTDATED));
      R->h_revision = (revision_t)L;
    }
}

static gcptr *FindRootsForLocalCollect(struct tx_descriptor *d)
{
  wlog_t *item;
  if (d->gcroots.size != 0)
    return d->gcroots.items;

  G2L_LOOP_FORWARD(d->global_to_local, item)
    {
      gcptr R = item->addr;
      gcptr L = item->val;
      assert(L->h_revision == (revision_t)R);
      if (L->h_tid & GCFLAG_NOT_WRITTEN)
        {
          L->h_tid |= GCFLAG_GLOBAL | GCFLAG_POSSIBLY_OUTDATED;
          continue;
        }
      gcptrlist_insert2(&d->gcroots, L, (gcptr)0);
    } G2L_LOOP_END;
  gcptrlist_insert(&d->gcroots, NULL);
  return d->gcroots.items;
}

int _FakeReach(gcptr P)
{
  if (P->h_tid & GCFLAG_GLOBAL)
    return 0;
  P->h_tid |= GCFLAG_GLOBAL | GCFLAG_NOT_WRITTEN;
  if ((P->h_tid & GCFLAG_LOCAL_COPY) == 0)
    P->h_revision = 1;
  return 1;
}

void CommitTransaction(void)
{
  revision_t cur_time;
  struct tx_descriptor *d = thread_descriptor;
  assert(d->active != 0);
  if (d->gcroots.size == 0)
    FindRootsForLocalCollect(d);   /* for tests */

  AcquireLocks(d);

  if (is_inevitable(d))
    {
      // no-one else can have changed global_cur_time if I'm inevitable
      cur_time = d->start_time;
      if (!bool_cas(&global_cur_time, INEVITABLE, cur_time + 2))
        {
          assert(!"global_cur_time modified even though we are inev.");
          abort();
        }
      inev_mutex_release();
    }
  else
    {
      while (1)
        {
          cur_time = global_cur_time;
          if (cur_time == INEVITABLE)
            {
              CancelLocks(d);
              inev_mutex_acquire();   // wait until released
              inev_mutex_release();
              AcquireLocks(d);
              continue;
            }
          if (bool_cas(&global_cur_time, cur_time, cur_time + 2))
            break;
        }
      // validate (but skip validation if nobody else committed)
      if (cur_time != d->start_time)
        if (!ValidateDuringCommit(d))
          AbortTransaction(2);
    }
  /* we cannot abort any more from here */
  d->setjmp_buf = NULL;
  gcptrlist_clear(&d->list_of_read_objects);
  g2l_clear(&d->global_to_local);
  fxcache_clear(&d->recent_reads_cache);

  UpdateChainHeads(d, cur_time);

  gcptrlist_clear(&d->gcroots);
  d->num_commits++;
  d->active = 0;
}

/************************************************************/

static void make_inevitable(struct tx_descriptor *d)
{
  d->setjmp_buf = NULL;
  d->active = 2;
  d->reads_size_limit_nonatomic = 0;
  update_reads_size_limit(d);
}

void BecomeInevitable(const char *why)
{
  revision_t cur_time;
  struct tx_descriptor *d = thread_descriptor;
  if (d == NULL || d->active != 1)
    return;  /* I am already inevitable, or not in a transaction at all
                (XXX statically we should know when we're outside
                a transaction) */

#ifdef RPY_STM_DEBUG_PRINT
  PYPY_DEBUG_START("stm-inevitable");
  if (PYPY_HAVE_DEBUG_PRINTS)
    {
      fprintf(PYPY_DEBUG_FILE, "%s\n", why);
    }
#endif

  inev_mutex_acquire();
  cur_time = global_cur_time;
  while (!bool_cas(&global_cur_time, cur_time, INEVITABLE))
    cur_time = global_cur_time;     /* try again */
  assert(cur_time != INEVITABLE);

  if (d->start_time != cur_time)
    {
      d->start_time = cur_time;
      if (!ValidateDuringCommit(d))
        {
          global_cur_time = cur_time;   // must restore the old value
          inev_mutex_release();
          AbortTransaction(3);
        }
    }
  make_inevitable(d);    /* cannot abort any more */

#ifdef RPY_STM_DEBUG_PRINT
  PYPY_DEBUG_STOP("stm-inevitable");
#endif
}

void BeginInevitableTransaction(void)
{
  struct tx_descriptor *d = thread_descriptor;
  revision_t cur_time;

  init_transaction(d);
  inev_mutex_acquire();
  cur_time = global_cur_time;
  while (!bool_cas(&global_cur_time, cur_time, INEVITABLE))
    cur_time = global_cur_time;     /* try again */
  assert(cur_time != INEVITABLE);
  d->start_time = cur_time;
  make_inevitable(d);
}

/************************************************************/

inline static gcptr GlobalizeForComparison(struct tx_descriptor *d, gcptr P)
{
  if (P != NULL && (P->h_tid & (GCFLAG_GLOBAL | GCFLAG_LOCAL_COPY)))
    {
      if (P->h_tid & GCFLAG_GLOBAL)
        P = LatestGlobalRevision(d, P, NULL, 0);
      else
        P = (gcptr)P->h_revision; // LOCAL_COPY: return the original global obj
    }
  return P;
}

_Bool stm_PtrEq(gcptr P1, gcptr P2)
{
  struct tx_descriptor *d = thread_descriptor;
  return GlobalizeForComparison(d, P1) == GlobalizeForComparison(d, P2);
}

/************************************************************/

int DescriptorInit(void)
{
  if (thread_descriptor == NULL)
    {
      struct tx_descriptor *d = malloc(sizeof(struct tx_descriptor));
      memset(d, 0, sizeof(struct tx_descriptor));

#ifdef RPY_STM_DEBUG_PRINT
      PYPY_DEBUG_START("stm-init");
#endif

      /* initialize 'my_lock' to be a unique odd number >= LOCKED */
      while (1)
        {
          d->my_lock = next_locked_value;
          if (bool_cas(&next_locked_value, d->my_lock, d->my_lock + 2))
            break;
        }
      if (d->my_lock < LOCKED)
        {
          /* XXX fix this limitation */
          fprintf(stderr, "XXX error: too many threads ever created "
                          "in this process");
          abort();
        }
      assert(d->my_lock & 1);
      thread_descriptor = d;

#ifdef RPY_STM_DEBUG_PRINT
      if (PYPY_HAVE_DEBUG_PRINTS)
        fprintf(PYPY_DEBUG_FILE, "thread %lx starting (pthread=%lx)\n",
                (long)d->my_lock, (long)pthread_self());
      PYPY_DEBUG_STOP("stm-init");
#endif
      return 1;
    }
  else
    return 0;
}

void DescriptorDone(void)
{
  struct tx_descriptor *d = thread_descriptor;
  assert(d != NULL);
  assert(d->active == 0);

  thread_descriptor = NULL;

#ifdef RPY_STM_DEBUG_PRINT
  PYPY_DEBUG_START("stm-done");
  if (PYPY_HAVE_DEBUG_PRINTS) {
    int num_aborts = 0, num_spinloops = 0;
    int i;
    char line[256], *p = line;

    for (i=0; i<ABORT_REASONS; i++)
      num_aborts += d->num_aborts[i];
    for (i=0; i<SPINLOOP_REASONS; i++)
      num_spinloops += d->num_spinloops[i];

    p += sprintf(p, "thread %lx: %d commits, %d aborts\n",
                 (long)d->my_lock,
                 d->num_commits,
                 num_aborts);

    for (i=0; i<ABORT_REASONS; i++)
      p += sprintf(p, "%c%d", i == 0 ? '[' : ',',
                   d->num_aborts[i]);

    for (i=1; i<SPINLOOP_REASONS; i++)  /* num_spinloops[0] == num_aborts */
      p += sprintf(p, "%c%d", i == 1 ? '|' : ',',
                   d->num_spinloops[i]);

    p += sprintf(p, "]\n");
    fwrite(line, 1, p - line, PYPY_DEBUG_FILE);
  }
  PYPY_DEBUG_STOP("stm-done");
#endif

  free(d);
}

#include "rpyintf.c"