Re: Using semaphores in PHP.
| From: | Tom May | Date: | Tue, 17 Nov 1998 01:40:34 +0000 |
| Subject: | Re: Using semaphores in PHP. | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-2433@lists.php.net to get a copy of this message | ||
Jim Winstead <jimw@php.net> writes:
> On Nov 16, Tom May wrote:
> > Any input on whether adding semaphore wrappers would be a reasonable
> > thing to do, and how to deal with the time limit problem (other than
> > not using a time limit)?
>
> Semaphores would be cool, especially if they were cross-platform. I would
> recommend looking at the resource-list management in PHP as a means of
> registering and cleaning up these semaphores. (Check out php3_list.h,
> list.c, and a module like db.c to see how these functions are used.)
The resource list stuff works great. I've implemented System V semaphore
wrappers with the following interface:
$sem = sem_get(int key, [int max_acquire]);
Gets or creates a System V semaphore and returns an id you can use to
access it, or false on failure The number of processes that can
acquire the semaphore simultaneously is set to max_acquire (defaults
to 1). Actually this value is set only if the process finds it is the
only process currently attached to the semaphore. The semaphore is
created with permissions = 666.
sem_acquire($sem);
Blocks (if necessary) until the semaphore can be acquired. Attempting
to acquire a semaphore that is currently acquired will block forever.
This behavior can easily be changed to allows calls to sem_acquire and
sem_release to nest. Returns true on success, false on failure.
sem_release($sem);
Releases the semaphore if it is currently acquired, otherwise logs an
error and returns false. Returns true on success, false on failure.
Semaphores are automatically released (if necessary) when a request is
finished.
I've done rudimentary testing on Solaris 2.6 and Linux 2.1.122. Note
that System V semaphores are required. I'll leave the cross-platform
stuff for someone else or until I need it on another platform myself
:-) It won't be easy to make it truly cross-platform. It may even be
impossible.
If you want it, cool, I can commit it along with the necessary changes
to configure.in, Makefile.in, config.h.in, and internal_functions.c.
I'll even attempt to do the documentation. If you don't want it,
fine, I can just dl() it and use it myself.
Tom.
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net