Commit f2eab6b8 authored by Éric Thiébaut's avatar Éric Thiébaut
Browse files

tao_attach_shared_object takes only one argument

parent c3d58c42
......@@ -6,8 +6,8 @@
// - TYPE must be defined with the C type name (without the tao_ prefix).
//
// - MAGIC can be defined with the magic type identifier to automatically
// generate code to attach and detach object of thsi type (not for basic
// shared objects).
// generate code to attach objects of this type (not for basic shared,
// remote, and rwlocked object types).
//
// - IS_REMOTE_OBJECT should be defined to non-zero for types derived from
// tao_remote_object and left undefined or defined to zero for other types.
......@@ -57,20 +57,26 @@
#endif
#ifdef MAGIC
TAO_XJOIN2(tao_,TYPE)* TAO_XJOIN3(tao_,TYPE,_attach)(
tao_shmid shmid)
{
return (TAO_XJOIN2(tao_,TYPE)*)tao_shared_object_attach(shmid, MAGIC);
tao_shared_object* obj = tao_shared_object_attach(shmid);
if (obj != NULL && obj->type == MAGIC) {
return (TAO_XJOIN2(tao_,TYPE)*)obj;
}
tao_shared_object_detach(obj);
tao_push_error(__func__, TAO_BAD_TYPE);
return NULL;
}
#endif /* MAGIC */
#if IS_RWLOCKED_OBJECT || IS_REMOTE_OBJECT
tao_status TAO_XJOIN3(tao_,TYPE,_detach)(
TAO_XJOIN2(tao_,TYPE)* obj)
{
return tao_shared_object_detach(tao_shared_object_cast(obj));
}
#endif /* MAGIC */
#endif /* IS_RWLOCKED_OBJECT || IS_REMOTE_OBJECT */
#if IS_SHARED_OBJECT
......
......@@ -230,8 +230,7 @@ tao_shared_object* tao_shared_object_create(
}
tao_shared_object* tao_shared_object_attach(
tao_shmid shmid,
uint32_t type)
tao_shmid shmid)
{
// Attach shared memory segment to the address space of the caller and
// check that the size and the type are consistent.
......@@ -249,10 +248,6 @@ tao_shared_object* tao_shared_object_attach(
tao_push_error(__func__, TAO_BAD_MAGIC);
goto detach;
}
if (type != TAO_SHARED_ANY && obj->type != type) {
tao_push_error(__func__, TAO_BAD_TYPE);
goto detach;
}
// Atomically increment the number of attachments and check whether object
// is about to be destroyed. Note that the `flags` member is constant and
......@@ -352,8 +347,20 @@ tao_rwlocked_object* tao_rwlocked_object_create(
return obj;
}
tao_rwlocked_object* tao_rwlocked_object_attach(
tao_shmid shmid)
{
tao_shared_object* obj = tao_shared_object_attach(shmid);
if (obj != NULL &&
(obj->type & TAO_SHARED_SUPERTYPE_MASK) == TAO_RWLOCKED_OBJECT) {
return (tao_rwlocked_object*)obj;
}
tao_shared_object_detach(obj);
tao_push_error(__func__, TAO_BAD_TYPE);
return NULL;
}
#define TYPE rwlocked_object
#define MAGIC TAO_RWLOCKED_OBJECT
#define IS_RWLOCKED_OBJECT 1
#include "./shared-methods.c"
......@@ -400,7 +407,19 @@ tao_remote_object* tao_remote_object_create(
return obj;
}
tao_remote_object* tao_remote_object_attach(
tao_shmid shmid)
{
tao_shared_object* obj = tao_shared_object_attach(shmid);
if (obj != NULL &&
(obj->type & TAO_SHARED_SUPERTYPE_MASK) == TAO_REMOTE_OBJECT) {
return (tao_remote_object*)obj;
}
tao_shared_object_detach(obj);
tao_push_error(__func__, TAO_BAD_TYPE);
return NULL;
}
#define TYPE remote_object
#define MAGIC TAO_REMOTE_OBJECT
#define IS_REMOTE_OBJECT 1
#include "./shared-methods.c"
......@@ -108,7 +108,6 @@ typedef enum tao_object_type_ {
//TAO_REMOTE_CAMERA = (TAO_REMOTE_OBJECT | 2),///< Remote camera.
TAO_REMOTE_MIRROR = (TAO_REMOTE_OBJECT | 3),///< Remote deformable mirror.
//TAO_REMOTE_SENSOR = (TAO_REMOTE_OBJECT | 4),///< Remote wavefront sensor.
TAO_SHARED_ANY = 0xffffffff ///< Any shared object.
} tao_object_type;
/**
......@@ -171,17 +170,13 @@ extern tao_shared_object* tao_shared_object_create(
*
* @param shmid Shared memory identifier.
*
* @param type Expected type identifier of the shared object. Can be
* `TAO_SHARED_ANY` to accept any shared object type.
*
* @return The address of the shared object in the address space of the caller;
* `NULL` in case of failure. Even tough the arguments are correct, an
* error may arise if the object has been destroyed before attachment
* completes.
*/
extern tao_shared_object* tao_shared_object_attach(
tao_shmid shmid,
uint32_t type);
tao_shmid shmid);
/**
* @brief Detach a shared object from the address space of the caller.
......
......@@ -14,6 +14,7 @@
#include <tao-locks.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
// To test process shared read/write locks, we launch several processes,
......@@ -171,7 +172,6 @@ static void run_writer(shared_data* data, long nframes)
static void run_reader(int shmid, int semidx)
{
tao_time time;
shared_data* data;
tao_serial previous = -1; // Previously processed frame number
long spurious = 0; // Number of spurious wake-up
long overflows = 0;
......@@ -181,8 +181,16 @@ static void run_reader(int shmid, int semidx)
int index;
// Attach to shared data.
data = (shared_data*)tao_shared_object_attach(shmid, SHARED_DATA);
shared_data* data = (shared_data*)tao_shared_object_attach(shmid);
if (data == NULL) {
tao_panic();
}
if (((tao_shared_object*)data)->type != SHARED_DATA) {
fprintf(stderr, "Invalid shared object type\n");
exit(EXIT_FAILURE);
}
// Run reader loop.
while (true) {
// Check for quitting condition and get index of next frame buffer.
lock_resources_for_reading(data);
......
......@@ -13,8 +13,9 @@
#include <tao-locks.h>
#include <tao-errors.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// To test process shared mutexes and condition variables, we launch two
// processes, the parent and a forked child, which communicate via a single
......@@ -95,11 +96,14 @@ static void run_producer(
tao_shmid shmid)
{
// Attach to shared data.
shared_data* data =
(shared_data*)tao_shared_object_attach(shmid, SHARED_DATA);
shared_data* data = (shared_data*)tao_shared_object_attach(shmid);
if (data == NULL) {
tao_panic();
}
if (((tao_shared_object*)data)->type != SHARED_DATA) {
fprintf(stderr, "Invalid shared object type\n");
exit(EXIT_FAILURE);
}
// Wait for the consumer to be ready. A first loop is to check for exit
// condition, a second is to wait for consumer expecting data. Spurious
......
......@@ -13,6 +13,7 @@
#include <tao-errors.h>
#include <tao-locks.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
......@@ -255,11 +256,14 @@ static void run_reader(
printf("Reader is starting...\n");
// Attach to shared data.
shared_data* data = (shared_data*)tao_shared_object_attach(
shmid, SHARED_DATA);
shared_data* data = (shared_data*)tao_shared_object_attach(shmid);
if (data == NULL) {
tao_panic();
}
if (((tao_shared_object*)data)->type != SHARED_DATA) {
fprintf(stderr, "Invalid shared object type\n");
exit(EXIT_FAILURE);
}
while (true) {
// Get index of next frame buffer.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment