Changeset 143


Ignore:
Timestamp:
Dec 3, 2006, 5:39:52 PM (19 years ago)
Author:
cinc
Message:

Made wpSetTitle()/wpQueryTitle() threadsafe by using atomic instructions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/desktop/class_c/wpobject.c

    r141 r143  
    201201{
    202202  WPObjectData* nomThis=WPObjectGetData(nomSelf);
    203 
    204   NOMString_assignString(_pnomStringTitle, pnomStrNewTitle, ev);
    205 
    206   return _pnomStringTitle;
     203  PNOMString tmpString=NOMStringNew();
     204  gpointer tmpPtr;
     205
     206  /* Create a new title */
     207  NOMString_assignString(tmpString, pnomStrNewTitle, ev);
     208
     209  /* It may happen that someone changed the title from another thread. We may either
     210     bail out then or just use our string as the mother of all strings. Since we are the last one
     211     trying to change the title our string is presumably the correct one. At least we act
     212     so :P. The memory holding the old string (which may not be the correct one anymore) is
     213     still valid because we hold a pointer to it in tmpPtr. Garbage collection won't free it
     214     under our feet.  */
     215  do{
     216    /* Get old NOMString containing old title */
     217    tmpPtr=g_atomic_pointer_get(&_pnomStringTitle);
     218  }while(!g_atomic_pointer_compare_and_exchange((gpointer*)&_pnomStringTitle, tmpPtr, (gpointer) tmpString));
     219
     220  return pnomStrNewTitle;
    207221}
    208222
    209223NOM_Scope PNOMString NOMLINK impl_WPObject_wpQueryTitle(WPObject* nomSelf, CORBA_Environment *ev)
    210224{
    211   WPObjectData* nomThis=WPObjectGetData(nomSelf);
    212 
    213   return _pnomStringTitle;
    214 }
    215 
     225  gpointer tmpPtr;
     226  WPObjectData* nomThis=WPObjectGetData(nomSelf);
     227
     228  /*
     229    This string will remain valid as long as we use it. Even if someone changes the title
     230    while we are in this method the garbage collector won't free it. The other thread
     231    changing the title is working on a copy so no problem here. */
     232  tmpPtr=g_atomic_pointer_get(&_pnomStringTitle);
     233
     234  return NOMString_copyString(tmpPtr, ev);
     235}
     236
Note: See TracChangeset for help on using the changeset viewer.