提交 9dd19122 编写于 作者: R Rich Felker

further optimize getc/putc when locking is needed

check whether the lock is free before loading the calling thread's
tid. if so, just use a dummy tid value that cannot compare equal to
any actual thread id (because it's one bit wider). this also avoids
the need to save the tid and pass it to locking_getc or locking_putc,
reducing register pressure.

this change might slightly hurt the case where the caller already
holds the lock, but it does not affect the single-threaded case, and
may significantly improve the multi-threaded case, especially on archs
where loading the thread pointer is disproportionately expensive like
early mips and arm ISA levels. but even on i386 it helps, at least on
some machines; I measured roughly a 10-15% improvement.
上级 7eda27d0
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__((__noinline__)) __attribute__((__noinline__))
#endif #endif
static int locking_getc(FILE *f, int tid) static int locking_getc(FILE *f)
{ {
if (a_cas(&f->lock, 0, tid)) __lockfile(f); if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f);
int c = getc_unlocked(f); int c = getc_unlocked(f);
if (a_swap(&f->lock, 0) & MAYBE_WAITERS) if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
__wake(&f->lock, 1, 1); __wake(&f->lock, 1, 1);
...@@ -15,8 +15,8 @@ static int locking_getc(FILE *f, int tid) ...@@ -15,8 +15,8 @@ static int locking_getc(FILE *f, int tid)
static inline int do_getc(FILE *f) static inline int do_getc(FILE *f)
{ {
int tid, l = f->lock; int l = f->lock;
if (l < 0 || (l & ~MAYBE_WAITERS) == (tid=__pthread_self()->tid)) if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid)
return getc_unlocked(f); return getc_unlocked(f);
return locking_getc(f, tid); return locking_getc(f);
} }
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__((__noinline__)) __attribute__((__noinline__))
#endif #endif
static int locking_putc(int c, FILE *f, int tid) static int locking_putc(int c, FILE *f)
{ {
if (a_cas(&f->lock, 0, tid)) __lockfile(f); if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f);
c = putc_unlocked(c, f); c = putc_unlocked(c, f);
if (a_swap(&f->lock, 0) & MAYBE_WAITERS) if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
__wake(&f->lock, 1, 1); __wake(&f->lock, 1, 1);
...@@ -15,8 +15,8 @@ static int locking_putc(int c, FILE *f, int tid) ...@@ -15,8 +15,8 @@ static int locking_putc(int c, FILE *f, int tid)
static inline int do_putc(int c, FILE *f) static inline int do_putc(int c, FILE *f)
{ {
int tid, l = f->lock; int l = f->lock;
if (l < 0 || (l & ~MAYBE_WAITERS) == (tid=__pthread_self()->tid)) if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid)
return putc_unlocked(c, f); return putc_unlocked(c, f);
return locking_putc(c, f, tid); return locking_putc(c, f);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册