Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
0 registered members (), 18,561 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Yet another bug #389724
12/17/11 14:47
12/17/11 14:47
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline 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...
Code:
#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...

Code:
#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.

Our new web site:Westmarch Studios
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 Offline
Expert
WretchedSid  Offline
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 Offline
Serious User
Rackscha  Offline
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: Rackscha] #389863
12/19/11 06:27
12/19/11 06:27
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline OP
User

Joined: Aug 2003
Posts: 902
Van Buren, Ar
wait is our friend if used well. The bug is that the value is not changed for the automatic variable... But I think that JustSid nailed the reason for this behavior and it is something that I had not considered. What I am doing is trying to work around the situation where I would have more that one instance of a function running and need to wait for a specific instance to finish. The macros wait_for and wait_for_my will not work in this case as more than one instance is running and the functions do not have an entity pointer. Guess I will have to use global memory and write a set of semaphore functions to manage a list of semaphores...

Thanks Guys


Our new web site:Westmarch Studios
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 Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
What about something like this:
Code:
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: WretchedSid] #389866
12/19/11 07:23
12/19/11 07:23
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline OP
User

Joined: Aug 2003
Posts: 902
Van Buren, Ar
That is about what I just did :-)

Code:
#ifndef SEMAPHORE_C
#define SEMAPHORE_C

#define MAX_SEMAPHORE 42


typedef struct {
	int semaphores[MAX_SEMAPHORE];
} ss_namespace_type;


ss_namespace_type ss_namespace;

void ss_startup(void)
{
	int i;
	
	for (i = 0; i < MAX_SEMAPHORE; i++) {
		ss_namespace.semaphores[i] = -1;
	}
}

int ss_Allocate()
{
	int i;
	
	for (i = 0; i < MAX_SEMAPHORE; i++) {
		if (ss_namespace.semaphores[i] == -1) {
			return(i);
		}
	}
	
	return(-1);
}

void ss_Release(int i)
{
	if (i < MAX_SEMAPHORE && i >= 0) {
		ss_namespace.semaphores[i] = -1;
	}
}

int ss_get(int i)
{
	if (i < MAX_SEMAPHORE && i >= 0) {
		return(ss_namespace.semaphores[i]);
	}
}
	
void ss_set(int i)
{
	if (i < MAX_SEMAPHORE && i >= 0) {
		ss_namespace.semaphores[i] = 1;
	}
}

void ss_clear(int i)
{
	if (i < MAX_SEMAPHORE && i >= 0) {
		ss_namespace.semaphores[i] = 0;
	}
}	

#define wait_semaphore(ws) while(!ss_get(ws)) wait(1)


#endif




Our new web site:Westmarch Studios
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 Offline
Expert
EvilSOB  Offline
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
Re: Yet another bug [Re: EvilSOB] #389882
12/19/11 13:06
12/19/11 13:06
Joined: Aug 2003
Posts: 902
Van Buren, Ar
Gordon Offline OP
User
Gordon  Offline OP
User

Joined: Aug 2003
Posts: 902
Van Buren, Ar
What I am doing is managing on screen effects in such a way as to not have global data. I have thought about doing just what you suggest but didn't want to risk incompatibility in the future. Anyway using a global semaphore has fixed the issue.


Our new web site:Westmarch Studios
Re: Yet another bug [Re: Gordon] #389889
12/19/11 14:11
12/19/11 14:11
Joined: Jul 2000
Posts: 28,024
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,024
Frankfurt
A comment about this issue - variables are saved by wait, but they are not in the same memory location - can be found in the wait documentation.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1