1 registered members (TipmyPip),
18,546
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Yet another bug
#389724
12/17/11 14:47
12/17/11 14:47
|
Joined: Aug 2003
Posts: 902 Van Buren, Ar
Gordon
OP
User
|
OP
User
Joined: Aug 2003
Posts: 902
Van Buren, Ar
|
the following code runs forever... I think a volatile key word would be able to fix this one...
#include <acknex.h>
#define PRAGMA_POINTER
void semaphorewait(int* semaphore)
{
*semaphore= 0;
wait(-2);
*semaphore= 1;
}
void main(void)
{
int semaphore;
semaphorewait(&semaphore);
wait(1);
while (semaphore== 0) {
wait(1);
}
sys_exit("");
}
this code works...
#define PRAGMA_POINTER
void semaphorewait(int* semaphore)
{
*semaphore= 0;
wait(-2);
*semaphore= 1;
}
int semaphore;
void main(void)
{
semaphorewait(&semaphore);
wait(1);
while (semaphore== 0) {
wait(1);
}
sys_exit("");
}
Last edited by Gordon; 12/17/11 14:48.
|
|
|
Re: Yet another bug
[Re: Gordon]
#389726
12/17/11 15:18
12/17/11 15:18
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
The problem here is wait, it will store the current variables on the stack when its called and will restore them later, however, its not guaranteed that the variables will end up on the same address as before, so your semaphore function will change something completely different and your sempahore variable will never change.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Yet another bug
[Re: WretchedSid]
#389831
12/18/11 22:19
12/18/11 22:19
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
wait is something you should absolutely AVOID between calls. I try to avoid wait anyway.
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
Re: Yet another bug
[Re: Gordon]
#389864
12/19/11 06:38
12/19/11 06:38
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
What about something like this:
typedef int* sempahore_t;
semaphore_t semaphoreCreate()
{
semaphore_t semaphore = malloc(sizeof(semaphore_t));
*semaphore = 0;
return semaphore;
}
void semaphoreDestroy(semaphore_t semaphore)
{
// You could try to lock the semaphore here and only destroy it once you obtained the lock
free(semaphore);
}
char semaphoreLocked(semaphore_t semaphore)
{
return (*semaphore == 0);
}
void semaphoreLock(semaphore_t semaphore)
{
*semaphore = 0;
wait(-2);
*semaphore = 1;
}
void main()
{
semaphore_t semaphore = semaphoreCreate();
semaphoreLock(semaphore);
while(semaphoreLocked(semaphore))
wait(1);
semaphoreDestroy(semaphore);
}
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Yet another bug
[Re: Gordon]
#389871
12/19/11 08:39
12/19/11 08:39
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
If "wait_for_my" nearly works, have you thought about using semaphore 'entities' in conjunction with 'wait_for_my' ?
That way you may be able to use just a single function, but running multiple instances, each with a different entity as an identifier...
(bear in mind I still do not REALLY understand what you are trying to achieve...)
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|