Back to index

glibc  2.9
stdio-lock.h
Go to the documentation of this file.
00001 /* Thread package specific definitions of stream lock type.  NPTL version.
00002    Copyright (C) 2000, 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _BITS_STDIO_LOCK_H
00021 #define _BITS_STDIO_LOCK_H 1
00022 
00023 #include <bits/libc-lock.h>
00024 #include <lowlevellock.h>
00025 
00026 
00027 /* The locking here is very inexpensive, even for inlining.  */
00028 #define _IO_lock_inexpensive       1
00029 
00030 typedef struct { int lock; int cnt; void *owner; } _IO_lock_t;
00031 
00032 #define _IO_lock_initializer { LLL_LOCK_INITIALIZER, 0, NULL }
00033 
00034 #define _IO_lock_init(_name) \
00035   ((_name) = (_IO_lock_t) _IO_lock_initializer , 0)
00036 
00037 #define _IO_lock_fini(_name) \
00038   ((void) 0)
00039 
00040 #define _IO_lock_lock(_name) \
00041   do {                                                               \
00042     void *__self = THREAD_SELF;                                             \
00043     if ((_name).owner != __self)                                     \
00044       {                                                                     \
00045        lll_lock ((_name).lock, LLL_PRIVATE);                                \
00046         (_name).owner = __self;                                             \
00047       }                                                                     \
00048     ++(_name).cnt;                                                   \
00049   } while (0)
00050 
00051 #define _IO_lock_trylock(_name) \
00052   ({                                                                 \
00053     int __result = 0;                                                       \
00054     void *__self = THREAD_SELF;                                             \
00055     if ((_name).owner != __self)                                     \
00056       {                                                                     \
00057         if (lll_trylock ((_name).lock) == 0)                                \
00058           {                                                          \
00059             (_name).owner = __self;                                         \
00060             (_name).cnt = 1;                                                \
00061           }                                                          \
00062         else                                                         \
00063           __result = EBUSY;                                          \
00064       }                                                                     \
00065     else                                                             \
00066       ++(_name).cnt;                                                 \
00067     __result;                                                        \
00068   })
00069 
00070 #define _IO_lock_unlock(_name) \
00071   do {                                                               \
00072     if (--(_name).cnt == 0)                                          \
00073       {                                                                     \
00074         (_name).owner = NULL;                                               \
00075        lll_unlock ((_name).lock, LLL_PRIVATE);                              \
00076       }                                                                     \
00077   } while (0)
00078 
00079 
00080 
00081 #define _IO_cleanup_region_start(_fct, _fp) \
00082   __libc_cleanup_region_start (((_fp)->_flags & _IO_USER_LOCK) == 0, _fct, _fp)
00083 #define _IO_cleanup_region_start_noarg(_fct) \
00084   __libc_cleanup_region_start (1, _fct, NULL)
00085 #define _IO_cleanup_region_end(_doit) \
00086   __libc_cleanup_region_end (_doit)
00087 
00088 #if defined _LIBC && !defined NOT_IN_libc
00089 
00090 # ifdef __EXCEPTIONS
00091 #  define _IO_acquire_lock(_fp) \
00092   do {                                                               \
00093     _IO_FILE *_IO_acquire_lock_file                                         \
00094        __attribute__((cleanup (_IO_acquire_lock_fct)))                      \
00095        = (_fp);                                                      \
00096     _IO_flockfile (_IO_acquire_lock_file);
00097 #  define _IO_acquire_lock_clear_flags2(_fp) \
00098   do {                                                               \
00099     _IO_FILE *_IO_acquire_lock_file                                         \
00100        __attribute__((cleanup (_IO_acquire_lock_clear_flags2_fct)))         \
00101        = (_fp);                                                      \
00102     _IO_flockfile (_IO_acquire_lock_file);
00103 # else
00104 #  define _IO_acquire_lock(_fp) _IO_acquire_lock_needs_exceptions_enabled
00105 #  define _IO_acquire_lock_clear_flags2(_fp) _IO_acquire_lock (_fp)
00106 # endif
00107 # define _IO_release_lock(_fp) ; } while (0)
00108 
00109 #endif
00110 
00111 #endif /* bits/stdio-lock.h */