1. RE: Semaphore help

jbrown105 at speedymail.org wrote:
> I have tried to implement the code for semaphores, but it seems
> not to be working. The main limitation is that fact that one of
> the paramters of a function (semctl) has a union as its type.
> How does one create/pass a union as a paramter to a C func?
> (It would be nice to also have examples of using semaphores, but
> I have the lpg (linux programmers guide) in addition to the
> man pages, so if none can be provided, I can manage.)

All a union is is a storage location that can contain different
size values. To use it all you need to do is know the largest
value that will fit in the union and this will be the size that
you will use when allocating the union size.
Then you can store any value that is listed in the union values.
in the same storage. The only thing you have to be careful
to use the correct size for the value when poking it into the storage.
I hope this makes sense.

Bernie

new topic     » topic index » view message » categorize

2. RE: Semaphore help

Hi JBrown/Bern,

Say it Euphoria-way would be a union is an object
In fact in C it is always(?) a pointer to  something 
like an Euphoria-object.

EUrs

antoine tammer

new topic     » goto parent     » topic index » view message » categorize

3. RE: Semaphore help

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C20CEF.D325EBB0
 charset=iso-8859-1

> -----Original Message-----
> From: jbrown105 at speedymail.org [mailto:jbrown105 at speedymail.org]
> Sent: Thursday, 6 June 2002 9:56
> To: EUforum
> Subject: Re: Semaphore help
> 
> 
> 
> On  0, Bernie Ryan <xotron at localnet.com> wrote:
> > 
> > 
> > jbrown105 at speedymail.org wrote:
> > > I have tried to implement the code for semaphores, but it seems
> > > not to be working. The main limitation is that fact that one of
> > > the paramters of a function (semctl) has a union as its type.
> > > How does one create/pass a union as a paramter to a C func?
> > > (It would be nice to also have examples of using semaphores, but
> > > I have the lpg (linux programmers guide) in addition to the
> > > man pages, so if none can be provided, I can manage.)
> > 
> > All a union is is a storage location that can contain different
> > size values. To use it all you need to do is know the largest
> > value that will fit in the union and this will be the size that
> > you will use when allocating the union size.
> > Then you can store any value that is listed in the union values.
> > in the same storage. The only thing you have to be careful
> > to use the correct size for the value when poking it into 
> the storage.
> > I hope this makes sense.
> > 
> > Bernie
> > 
> 
> It does. However I am unclear as how to define a C func which
> uses a union.
> 
>        union semun {
>                int val;                    /* value for SETVAL */
>                struct semid_ds *buf;       /* buffer for IPC_STAT,
>                IPC_SET */
>                unsigned short int *array;  /* array for GETALL, SETALL
>                */
>                struct seminfo *__buf;      /* buffer for IPC_INFO */
>        };
> 
>        int semctl (int  semid, int semnum, int cmd, union semun arg);
> 
> Thats the C prototype. How do I use define_c_func() to have a union
> as a parameter? I got this far:
> 
> semctl_ = define_c_func(so, "semctl", {C_INT, C_INT, C_INT, }, C_INT)
> 
> How do I finish it?
> 

semctl_ = define_c_func(so, "semctl", {C_INT, C_INT, C_INT, C_POINTER},
C_INT)

Because the largest item in the union is a pointer.

When calling this routine, you just need to decide what you are sending in
the last parameter. It is expecting one of ...
 val
 address of buf
 address of array
 address of __buf

depending on what the cmd value is.

-----------
Derek.

==================================================================
De informatie opgenomen in dit bericht kan vertrouwelijk zijn en 
is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht 
onterecht ontvangt wordt u verzocht de inhoud niet te gebruiken en 
de afzender direct te informeren door het bericht te retourneren. 
==================================================================
The information contained in this message may be confidential 
and is intended to be exclusively for the addressee. Should you 
receive this message unintentionally, please do not use the contents 
herein and notify the sender immediately by return e-mail.


==================================================================

------_=_NextPart_000_01C20CEF.D325EBB0
Content-Type: application/ms-tnef

new topic     » goto parent     » topic index » view message » categorize

4. RE: Semaphore help

jbrown105 at speedymail.org wrote:
> On  0, Bernie Ryan <xotron at localnet.com> wrote:
> > 
> > 
> > jbrown105 at speedymail.org wrote:
> > > I have tried to implement the code for semaphores, but it seems
> > > not to be working. The main limitation is that fact that one of
> > > the paramters of a function (semctl) has a union as its type.
> > > How does one create/pass a union as a paramter to a C func?
> > > (It would be nice to also have examples of using semaphores, but
> > > I have the lpg (linux programmers guide) in addition to the
> > > man pages, so if none can be provided, I can manage.)
> > 
> > All a union is is a storage location that can contain different
> > size values. To use it all you need to do is know the largest
> > value that will fit in the union and this will be the size that
> > you will use when allocating the union size.
> > Then you can store any value that is listed in the union values.
> > in the same storage. The only thing you have to be careful
> > to use the correct size for the value when poking it into the storage.
> > I hope this makes sense.
> > 
> > Bernie
> > 
> 
> It does. However I am unclear as how to define a C func which
> uses a union.
> 
>        union semun {
>                int val;                    /* value for SETVAL */
>                struct semid_ds *buf;       /* buffer for IPC_STAT,
>                IPC_SET */
>                unsigned short int *array;  /* array for GETALL, SETALL
>                */
>                struct seminfo *__buf;      /* buffer for IPC_INFO */
>        };
> 
>        int semctl (int  semid, int semnum, int cmd, union semun rg);
> 
> Thats the C prototype. How do I use define_c_func() to have a union
> as a parameter? I got this far:
> 
> semctl_ = define_c_func(so, "semctl", {C_INT, C_INT, C_INT, }, C_INT)
> 
> How do I finish it?
> 
> TIA,
> jbrown

semctl_ = define_c_func(so, "semctl", {C_INT, C_INT, C_UINT, },C_INT)

The largest value in the union is a pointer which needs a size of
 C_UINT ( 4 bytes  unsigned integer ) in the last parameter.

 the C_INT tells Euphoria to only allow a signed value
 if you use C_UINT then let Euphoria think that any 
 negative number is just a large positive number.
 If that doesn't work then just create two different functions
 with diferent parameter types but the same name.

semctl_int = define_c_func(so, "semctl", {C_INT, C_INT, C_INT, },C_INT)


semctl_pointer = define_c_func(so, "semctl", {C_INT, C_INT, C_UINT, 
},C_INT)
 
 Euphoria is using the the definition to determine what the
 sign of a parameter, the size in bytes, and the number of
 arguments. Then it looks up the name of the function in the SO/DLL
 and sees if it can finds its name and grabs a pointer to it.
 "C" will use the parameters depending how euphoria puts them
 on the stack and Euphoria will use the definition to type check
 parameters that you are using.


Bernie

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu