Implement Register, which will handle adding descriptors and callbacks to the hub

This commit is contained in:
R. Tyler Croy 2011-02-12 12:20:25 -08:00
parent 62614d9424
commit 966cb4feab
2 changed files with 49 additions and 7 deletions

View File

@ -1,10 +1,30 @@
--
-- epoll.adb - Loose binding on top of the epoll(7) interface in the Linux
-- kernel
--
package body Epoll is
procedure Register (This : in Hub; Descriptor : in C.int) is
procedure Register (This : in out Hub; Descriptor : in C.int; Cb : in Callback) is
Event : aliased Epoll_Event;
begin
null;
Validate_Hub (This);
Event.Events := EPOLLIN;
Event.Data.Fd := Descriptor;
declare
Status : C.int;
begin
Status := Epoll_Ctl (This.Epoll_fd, EPOLL_CTL_ADD, Descriptor, Event'Access);
if Status = -1 then
raise Descriptor_Registration_Falied;
end if;
end;
Callback_Registry.Insert (This.Callbacks, Descriptor, Cb);
end Register;
procedure Run (This : in Hub) is
@ -29,4 +49,11 @@ package body Epoll is
return Created_Hub;
end Create;
procedure Validate_Hub (H : in Hub) is
begin
if H.Epoll_Fd < 0 then
raise Hub_Invalid;
end if;
end Validate_Hub;
end Epoll;

View File

@ -4,29 +4,44 @@
with Interfaces.C;
with System;
with Ada.Containers.Vectors;
use Ada.Containers;
use Interfaces;
package Epoll is
type Callback is access procedure (Descriptor : C.int);
-- My_Callback.all (Fd);
package Callback_Registry is new Vectors (C.int, Callback);
type Hub is tagged private;
procedure Register (This : in Hub; Descriptor : C.int);
procedure Register (This : in out Hub;
Descriptor : in C.int;
Cb : in Callback);
procedure Run (This : in Hub);
function Create return Hub;
Hub_Create_Failed : exception;
Hub_Invalid : exception;
Descriptor_Registration_Falied : exception;
private
use Interfaces.C;
-- Callback_Registry will be used to determine "who" to invoke when a
-- specific descriptor receives data in the `Run` loop
type Hub is tagged record
Epoll_Fd : C.int := -1;
Should_Continue : Boolean := True;
Callbacks : Callback_Registry.Vector;
end record;
procedure Validate_Hub (H: in Hub);
subtype EPOLL_CTL_OPS is C.unsigned;
EPOLL_CTL_ADD : constant EPOLL_CTL_OPS := 1;
EPOLL_CTL_DEL : constant EPOLL_CTL_OPS := 2;
@ -58,18 +73,18 @@ package Epoll is
when Pointer =>
Ptr : System.Address;
when File_Descriptor =>
Fd : C.int;
Fd : aliased C.int;
when Int_32 =>
u32 : C.int;
u32 : aliased C.int;
when Int_64 =>
u64 : C.int;
u64 : aliased C.int;
end case;
end record;
pragma Convention (C_Pass_By_Copy, Epoll_Data);
pragma Unchecked_Union (Epoll_Data);
type Epoll_Event is record
Events : Integer;
Events : aliased C.unsigned;
Data : Epoll_Data;
end record;
pragma Convention (C_Pass_By_Copy, Epoll_Event);