Changeset 16067


Ignore:
Timestamp:
May 1, 2014, 4:54:19 PM (6 years ago)
Author:
rme
Message:

Remove the Darwin-specific code that managed a freelist of TCRs,
and just use malloc and free instead.

Location:
trunk/source/lisp-kernel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/lisp-kernel/memory.c

    r15853 r16067  
    914914#endif
    915915}
    916 
    917 
    918    
    919 
    920  
    921 #ifdef DARWIN
    922 /*
    923   On 64-bit Darwin, we try to make a TCR's address serve as a Mach port
    924   name, which means that it has to fit in 32 bits (and not conflict with
    925   an existing port name, but that's a separate issue.)  Darwin doesn't
    926   seem to offer means of mapping/allocating memory that's guaranteed to
    927   return a 32-bit address on 64-bit systems, and trial-and-error doesn't
    928   scale well.
    929  
    930   Since it's a PITA to allocate 32-bit TCR pointers, we never free them
    931   once we've done so.  (We maintain a queue of "freed" TCRs but never
    932   unmap the memory.)  When we need to allocate TCR pointers, we try to
    933   allocate substantially more than we need.
    934 
    935   The bulk allocation works by scanning the task's mapped memory
    936   regions until a free region of appropriate size is found, then
    937   mapping that region (without the dangerous use of MAP_FIXED).  This
    938   will win if OSX's mmap() tries to honor the suggested address if it
    939   doesn't conflict with a mapped region (as it seems to in practice
    940   since at least 10.5 and as it's documented to in 10.6.)
    941 
    942   OSX 10.8 introduces new horrors that affect 32-bit CCL as well:
    943 
    944   mach_port_allocate_name(mach_task_self(),MACH_PORT_RIGHT_RECEIVE,n)
    945  
    946   returns KERN_NO_SPACE for n > ~#x09800000.  It's not known whether or
    947   not this is intentional; even if it's a bug, it suggests that we should
    948   probably stop trying to arrange that a TCR's address can be used as the
    949   corresponding thread's exception port and maintain some sort of
    950   efficient and thread-safe mapping from port to TCR.  Soon.
    951 
    952   News flash:  mach_port_allocate_name() is not only worse than we
    953   imagine on 10.8, but it's worse than we can imagine.  Give up.
    954   (This means that there are no longer any constraints on TCR addresses
    955   and we could just use malloc here, but keep some of this code around
    956   for now.)
    957 */
    958 
    959 pthread_mutex_t darwin_tcr_lock = PTHREAD_MUTEX_INITIALIZER;
    960 
    961 TCR _free_tcr_queue, *darwin_tcr_freelist=&_free_tcr_queue;
    962 
    963 #define TCR_CLUSTER_COUNT 1024   /* Enough that we allocate clusters rarely,
    964 but not so much that we waste lots of 32-bit memory. */
    965 
    966 
    967 
    968 
    969 /* force 16-bit alignment, just in case */
    970 typedef struct {
    971   TCR tcr;
    972 }  __attribute__ ((aligned(16))) MTCR;
    973 
    974 
    975 
    976 void
    977 link_tcr_list(TCR *head, MTCR *buf, int n)
    978 {
    979   TCR *prev = head, *tcr;
    980   int i;
    981 
    982   for (i=0; i < n; i++, buf++) {
    983     tcr = &(buf->tcr);
    984     prev->next = tcr;
    985     tcr->prev = prev;
    986     head->prev = tcr;
    987     tcr->next = head;
    988     prev = tcr;
    989   }
    990 }
    991 
    992 
    993 
    994 
    995 void
    996 map_tcr_cluster(TCR *head)
    997 {
    998   MTCR *work = NULL;
    999   TCR *prev = head;
    1000   int i;
    1001   size_t request_size = align_to_power_of_2((TCR_CLUSTER_COUNT*sizeof(MTCR)),log2_page_size);
    1002 
    1003   work = (MTCR *)mmap(NULL,
    1004                       request_size,
    1005                       PROT_READ|PROT_WRITE,
    1006                       MAP_PRIVATE|MAP_ANON,
    1007                       -1,
    1008                       0);
    1009 
    1010   if (work == MAP_FAILED) {
    1011     Fatal("Can't allocate memory for thread-local storage.", "");
    1012   }
    1013   link_tcr_list(head, work, TCR_CLUSTER_COUNT);
    1014 }
    1015 
    1016 void
    1017 darwin_free_tcr(TCR *tcr)
    1018 {
    1019   TCR  *head = darwin_tcr_freelist, *tail;
    1020 
    1021   pthread_mutex_lock(&darwin_tcr_lock);
    1022   tail = head->prev;
    1023   tail->next = tcr;
    1024   head->prev = tcr;
    1025   tcr->prev = tail;
    1026   tcr->next = head;
    1027   pthread_mutex_unlock(&darwin_tcr_lock);
    1028 }
    1029 
    1030 
    1031 TCR *
    1032 darwin_allocate_tcr()
    1033 {
    1034   TCR  *head = darwin_tcr_freelist, *tail, *tcr;
    1035   pthread_mutex_lock(&darwin_tcr_lock);
    1036   if (head->next == NULL) { /* First time */
    1037     head->next = head->prev = head;
    1038   }
    1039 
    1040   if (head->next == head) {
    1041     map_tcr_cluster(head);
    1042   }
    1043   tcr = head->next;
    1044   tail = tcr->next;
    1045   tail->prev = head;
    1046   head->next = tail;
    1047   pthread_mutex_unlock(&darwin_tcr_lock);
    1048   memset(tcr,0,sizeof(TCR));
    1049   return tcr;
    1050 }
    1051  
    1052 
    1053 
    1054 
    1055 #endif
  • trunk/source/lisp-kernel/thread_manager.c

    r16060 r16067  
    850850allocate_tcr()
    851851{
    852   TCR *tcr,  *next;
    853 #ifdef DARWIN
    854   extern TCR* darwin_allocate_tcr(void);
    855   extern void darwin_free_tcr(TCR *);
    856 #endif
    857   for (;;) {
    858 #ifdef DARWIN
    859     tcr = darwin_allocate_tcr();
    860 #else
    861     tcr = calloc(1, sizeof(TCR));
    862 #endif
    863     return tcr;
    864   }
     852  TCR *tcr;
     853
     854  tcr = calloc(1, sizeof(TCR));
     855  if (tcr == NULL)
     856    allocation_failure(true, sizeof(TCR));
     857  return tcr;
    865858}
    866859#endif
     
    13591352shutdown_thread_tcr(void *arg)
    13601353{
    1361 #ifdef DARWIN
    1362   extern void darwin_free_tcr(TCR *);
    1363 #endif
    13641354  TCR *tcr = TCR_FROM_TSD(arg),*current=get_tcr(0);
    13651355
     
    14451435#endif
    14461436#ifdef DARWIN
    1447     darwin_free_tcr(tcr);
     1437    memset(tcr, 0, sizeof(TCR));
     1438    free(tcr);
    14481439#endif
    14491440    UNLOCK(lisp_global(TCR_AREA_LOCK),current);
     
    22902281free_freed_tcrs ()
    22912282{
    2292 #ifdef DARWIN
    2293   extern void darwin_free_tcr(TCR *);
    2294 #endif
    22952283  TCR *current, *next;
    22962284
     
    23022290     * tcr aux vector elsewhere. */
    23032291#else
    2304 #ifdef DARWIN
    2305     darwin_free_tcr(current);
    2306 #else
    23072292    free(current);
    2308 #endif
    23092293#endif
    23102294#endif
Note: See TracChangeset for help on using the changeset viewer.