interpreterProxy signalSemaphoreWithIndex:

Gerardo Richarte gera at corest.com
Fri Jun 9 21:26:45 UTC 2006


lethalman88 said:
> So, does #signalSemaphoreWithIndex: work on Windows and other platforms?

	Yes it does. It's a feature of the VM (the interpreter), and it's 100% platform independent. In fact, if there is no platform it also works (we are using it in SqueakNOS).

	David explained why you may want to use it. The counterpart is how to use it:

In Squeak at some point you'll want to wait for an external event, for this, you create a Semaphore and register it:

<code>
sem := Semaphore new.
plugin registerSemaphore: (Smalltalk registerExternalObject: sem).
</code>

With #registerExternalObject: you register the object so it can be accessed from the native world. This is necesary because objects tend to move with Garbage Collection, so if you just used a direct pointer to the object, this pointer would become obsolete when the GC moves the object.  #registerExternalObject: puts the object in an array (updated by the GC I guess) and returns an index.

your plugin will need to implement something like #registerSemaphore:, for example for SqueakNOS we have:

<code>
SqueakNOSPlugin>>primitiveRegisterSemaphoreIndex: aSemaphoreIndex forIRQ: irqNumber
	| IRQSemaphores |

	self primitive: 'primitiveRegisterSemaphoreIndexForIRQ'
		parameters: #(SmallInteger SmallInteger).

	self var: #IRQSemaphores type: 'extern t_IRQSemaphores'.

	irqNumber < (IRQSemaphores sizeof/(IRQSemaphores at: 0) sizeof)
			ifTrue: [IRQSemaphores at: irqNumber put: aSemaphoreIndex]
			ifFalse: [interpreterProxy primitiveFail].

	^ interpreterProxy trueObject
</code>

	where t_IRQSemaphores is nothing more than an array of integers. So we save the index returned by #registerExternalObject: in this array for when we want to use it.

	When the external even hapens (your native part will know), you need to call #signalSemaphoreWithIndex:, for example:
<code>
	void signalIRQ(int number) {
		if (0 != IRQSemaphores[number])
		  signalSemaphoreWithIndex(IRQSemaphores[number]);
	}		
</code>

and inside Squeak you will have a Process waiting on the Semaphore, for example, the complite code could be:

<code>
sem := Semaphore new.
self primRegisterSemaphore: (Smalltalk registerExternalObject: sem).

[[
	sem wait.
	self doSomething] repeat.
] fork.
</code>

	Hope it helps.

	gera

</code>





More information about the Squeak-dev mailing list