On Wednesday 25 June 2003 15:56, Angelo Dell'Aera wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Tue, 24 Jun 2003 20:07:57 +0200 > > Frank.Uepping@t-online.de (Frank A. Uepping) wrote: > >Imagine we have a service which we can use by registering a callback > > function. The registered callback is then called whenever the service > > decides so. When we are no more interested in the service we release the > > callback. > > > >E.g.: > > > >struct X { > > void (*callback)(void *data); > > ... > >}; > > > >void register_x(struct X *x, void *data); // data is passed to > > x.callback() void release_x(struct X* x); > > > >I think we could get in trouble on SMP machines with that. > >It could be that one thread is about to call callback() while > >another thread is about to call release_x(). > >If release_x() returns before callback() is finished and > >the subsequent code following release_x() invalidates data > >(e.g. kfree(data)) callback() works on with an invalid data ptr > >-- havoc is the result. > > > >How to make this SMP safe? > >Yes we can use spinlocks to make release_x() wait while callback() > >is in execution. But this has the consequence that callback() must be > >fast/non-blocking (holding a spinlock a longer time is not permitted). > > > >Are there any other solutions? > > A simple solution you can use is a mutex. > > Simply do this > > struct semaphore sem; > > void release_x(struct foo *foo) > { > down_interruptible(&sem); > > CRITICAL CODE SECTION > > up(&sem); > } > > void callback(void *data) > { > down_interruptible(&sem); > > CRITICAL CODE SECTION > > up(&sem); > } > This didn't work well!!! Data could be invalidated while callback() is waiting for sem. /FAU -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/