简体   繁体   中英

How or where is the thread-specific data predefined structure set on 64 bit OSX?

From what I've found so far the "gs" register points to a thread-specific area of data (TSD). Memory of the size of one pointer can be allocated in this area by using pthread_key_create and be written to using pthread_setspecific.

After looking at apple's tsd.h there also seem to be some entries of the same size (pointer-sized) predefined at the beginning of the TSD memory area, namely __TSD_THREAD_SELF, __TSD_ERRNO, __TSD_MIG_REPLY and __TSD_SEMAPHORE_CACHE.

So now my questions are:

  • are these entries all of the predefined structure?
  • where does pthread_key_create reserve memory for a new entry? (old versions use _PTHREAD_TSD_OFFSET as offset from the address in the "gs" register but I cannot find it in the new OSX sources)
  • where is the sourcecode that initializes the predefined structure?
  • if there is still a _PTHREAD_TSD_OFFSET and if it is still much bigger than __TSD_RESERVED_MAX * sizeof(pointer), then what is the space in between those used for?

(since OSX binaries use mach-o, dyld seems to be involved in this as well somehow)

This is defined in the libpthread subcomponent . In particular, look at the struct _pthread in internal.h . The tsd field is the array of thread-specific data that gsbase points to. The first _INTERNAL_POSIX_THREAD_KEYS_MAX (currently 256) elements are reserved to Apple. The remaining _EXTERNAL_POSIX_THREAD_KEYS_MAX (currently 512 on macOS) elements are for callers of pthread_key_create() .

The internal slots are reserved for various system library/framework and C library purposes. Some of the reservations are documented in tsd_private.h .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM