Sharing memory between applications, server/creator side.
Sharing memory between applications, server/creator side.
#include <l4/re/util/object_registry>
#include <l4/util/util.h>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>
#include <pthread-l4.h>
#include <thread>
#include "interface.h"
{
private:
public:
: _shm(shm), _irq(irq)
{}
};
{
(void) obj;
ios >> t;
{
case L4::Meta::Protocol:
return L4::Util::handle_meta_request<My_interface>(ios);
case 0:
ios << _shm << _irq;
return 0;
default:
}
}
{
private:
char *_shm;
public:
explicit Shm_observer(char *shm)
: _shm(shm)
{}
};
{
(void)obj;
(void)ios;
printf("Client sent us: %s\n", _shm);
return 0;
}
enum
{
DS_SIZE = 4 << 12,
};
{
if (!(*_ds).is_valid())
{
printf("Dataspace allocation failed.\n");
return 0;
}
if (err < 0)
{
printf("mem_alloc->alloc() failed.\n");
return 0;
}
char *_addr = 0;
if (err < 0)
{
printf("Error attaching data space: %s\n", l4sys_errtostr(err));
return 0;
}
printf("Attached DS\n");
static char const * const msg = "[DS] Hello from server!";
snprintf(_addr, strlen(msg) + 1, msg);
return _addr;
}
static void *server_thread(void *)
{
char *addr;
if (!(addr = get_ds(&ds)))
return nullptr;
Shm_observer observer(addr);
My_server_obj server_obj(ds, irq);
server.registry()->register_obj(&server_obj, "shm");
server.loop();
}
int main()
{
pthread_attr_t pattr;
if (pthread_attr_init(&pattr))
pthread_t thr;
L4Re::chksys(pthread_create(&thr, &pattr, server_thread,
nullptr),
"Create server thread");
L4Re::chksys(pthread_attr_destroy(&pattr),
"Destroy pthread attributes");
return 0;
}
Interface for memory-like objects.
static Env const * env() noexcept
Returns the initial environment for the current task.
L4::Cap< Mem_alloc > mem_alloc() const noexcept
Object-capability to the memory allocator.
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
A server loop object which has a Object_registry included.
C++ interface for capabilities.
Input/Output stream for IPC [un]marshalling.
virtual int dispatch(unsigned long rights, Ipc::Iostream &ios)=0
The abstract handler for client requests to the object.
unsigned long l4_umword_t
Unsigned machine word.
@ L4_EBADPROTO
Unsupported protocol.
_Cap_alloc & cap_alloc
Capability allocator.
void l4_sleep_forever(void) L4_NOTHROW L4_NORETURN
Go sleep and never wake up.
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
void throw_error(long err, char const *extra="")
Generate C++ exception.
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
@ RW
Readable and writable region.
@ Search_addr
Search for a suitable address range.
Server object base class for handling IRQ messages.
Base class (template) for server implementing server objects.
Message tag data structure.
long label() const L4_NOTHROW
Get the protocol value.
Type information server template.