Created
August 8, 2010 06:43
-
-
Save RTfXGaGeqSeItbMUgpFlekUs/513687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(regs). | |
-export([create/1, set/3, get/2, regs/1, start/0]). | |
%%% Create a list of registers. | |
create(N) -> | |
create([], 0, N). | |
create(R, _, 0) -> | |
R; | |
create(R, C, N) -> | |
create(R ++ [{C, 0}], C+1, N-1). | |
%%% Set data in register N. | |
set(R, N, Data) -> | |
lists:keystore(N, 1, R, {N, Data}). | |
%%% Get data in register N. | |
get(R, N) -> | |
{_, Data} = lists:keyfind(N, 1, R), | |
Data. | |
%%% Main state machine. | |
regs(R) -> | |
receive | |
{From, get, {N}} -> | |
From ! {self(), response, {get, get(R, N)}}, | |
regs(R); | |
{_From, set, {N, Data}} -> | |
regs(set(R, N, Data)); | |
terminate -> | |
ok | |
end. | |
%%% Starts a process with N registers. | |
start(N) -> | |
spawn(regs, regs, [create(N)]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment