Exec

# Standard types

## Integral types

 BYTE
 UWORD
 WORD
 ULONG
 LONG
 UQUAD
 QUAD

## Text types

 WCHAR
 STRPTR
 CONST\_STRPTR
 WSTRPTR
 CONST\_WSTRPTR

## Other types

 IPTR
 APTR
 CONST\_APTR

# Memory allocation

## Memory types

 MEMF\_CHIP
 MEMF\_FAST

## Memory attributes

 MEMF\_CLEAR
 MEMF\_LOCAL
 MEMF\_NO\_EXPUNGE
 MEMF\_PUBLIC
 MEMF\_REVERSE

## Allocating memory

```
struct MsgPort \*port = AllocMem(sizeof(\*port), MEMF\_ANY);

```

## Freeing memory

```
if (port)
 FreeMem(port, sizeof(\*port));

```

# Messages and ports

## Create message port

There are two ways to create message port: create it manually or use CreateMsgPort() call.

```
 struct MsgPort \*port = CreateMsgPort();

```
If you want to create it manually you can use following code:

```
struct MsgPort \*CreatePort()
{
 struct MsgPort \*port = AllocMem(sizeof(\*port), MEMF\_ANY);

 if (port)
 {
 NEWLIST(&port->mp\_MsgList);

 port->mp\_Flags = PA\_SIGNAL;
 port->mp\_SigBit = AllocSignal(-1);
 port->mp\_SigTask = SysBase->ThisTask;

 if (port->mp\_SigBit == (UBYTE)-1)
 {
 FreeMem(port, sizeof(\*port));
 return NULL;
 }
 }

 return port;
}

```

## Delete message port

If you created message port using CreageMsgPort():

```
 DeleteMsgPort(port);

```

If it was created manually:

```
void DeletePort(struct MsgPort \*port)
{
 if (port)
 {
 FreeSignal(port->mp\_SigBit);
 FreeMem(port, sizeof(\*port));
 }
}

```

## Sending a message

```
void SendMessage(struct MsgPort \*source, struct MsgPort \*destination)
{
 struct Message \*msg = AllocMem(sizeof(\*msg), MEMF\_ANY);

 if (msg)
 {
 msg->mn\_ReplyPort = source;
 PutMsg(destination, &msg);
 WaitPort(source);
 GetMsg(source);
 FreeMem(msg, sizeof(\*msg));
 }
}

```

## Example

```
#include <exec/execbase.h>
#include <exec/ports.h>
#include <proto/exec.h>

static struct Message \*WaitMessage(struct MsgPort \*port)
{
 WaitPort(port);
 return GetMsg(port);
}

int main()
{
 struct MsgPort \*port1, \*port2;

 port1 = CreateMsgPort();
 port2 = CreateMsgPort();

 if (port1 && port2)
 {
 // Send a message to port 2...
 struct Message msg, \*m;
 msg.mn\_ReplyPort = port1;
 PutMsg(port2, &msg);

 // Wait for a message at port 2...
 m = WaitMessage(port2);

 // Reply message back to sender
 ReplyMsg(m);

 // Wait for a reply at port 1
 WaitMessage(port1);
 }

 DeleteMsgPort(port1);
 DeleteMsgPort(port2);

 return 0;
}

```

# Semaphores

Semaphores are used to protect critical sections.

## Creating semaphore

```
struct SignalSemaphore \*CreateSemaphore()
{
 struct SignalSemaphore \*sem = AllocMem(sizeof(\*sem), MEMF\_ANY | MEMF\_CLEAR);

 if (sem)
 {
 InitSemaphore(sem);
 }

 return sem;
}

```

## Delete semaphore

```
void DeleteSemaphore(struct SignalSemaphore \*sem)
{
 if (sem)
 FreeMem(sem, sizeof(\*sem));
}

```

## Function reference

### AttemptSemaphore

Attempt to get an exclusive access to the semaphore.

```
AttemptSemaphore(semaphore);

```

### ObtainSemaphore

Obtain an exclusive access to the semaphore. This call blocks if semaphore is not available.

```
ObtainSemaphore(semaphore);

```

### ObtainSemaphoreShared

Obtain a shared access to the semaphore. This call blocks if semaphore is not available.

```
ObtainSemaphoreShared(semaphore);

```

### ReleaseSemaphore

Release shared or exclusive access to the semaphore.

```
ReleaseSemaphore(semaphore);

```

## Example

```
#include <exec/semaphores.h>
#include <proto/exec.h>

int main()
{
 struct SignalSemaphore \*sem = AllocMem(sizeof(\*sem), MEMF\_ANY | MEMF\_CLEAR);

 if (sem != NULL)
 {
 InitSemaphore(sem);
 ObtainSemaphore(sem);

 // Protected area...

 ReleaseSemaphore(sem);
 FreeMem(sem, sizeof(\*sem));
 }

 return 0;
}

```