{"id":20372782,"url":"https://github.com/sidmishraw/santa-claus-go","last_synced_at":"2025-08-19T03:02:50.488Z","repository":{"id":86400725,"uuid":"112297207","full_name":"sidmishraw/santa-claus-go","owner":"sidmishraw","description":"An implementation of the Santa Claus problem as defined in the Beautiful concurrency using my reworked Go STM library","archived":false,"fork":false,"pushed_at":"2017-12-07T01:42:46.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T20:43:22.629Z","etag":null,"topics":["beautiful-concurrency","go","golang","santa-claus-problem","software-transactional-memory","stm"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sidmishraw.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-28T06:45:42.000Z","updated_at":"2021-07-07T06:35:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"8a721890-ee5e-45f2-804f-b634e56365fd","html_url":"https://github.com/sidmishraw/santa-claus-go","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sidmishraw/santa-claus-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2Fsanta-claus-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2Fsanta-claus-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2Fsanta-claus-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2Fsanta-claus-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sidmishraw","download_url":"https://codeload.github.com/sidmishraw/santa-claus-go/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2Fsanta-claus-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271092828,"owners_count":24697914,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-19T02:00:09.176Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beautiful-concurrency","go","golang","santa-claus-problem","software-transactional-memory","stm"],"created_at":"2024-11-15T01:14:53.108Z","updated_at":"2025-08-19T03:02:49.652Z","avatar_url":"https://github.com/sidmishraw.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The Santa Claus Problem implementation using my reworked Go STM\n\n\u003e Author: Sidharth Mishra\n\n## Description\n\nThe problem is stated as follows:\n\n```text\nSanta repeatedly sleeps until wakened by either all of his nine reindeer, back from their\nholidays, or by a group of three of his ten elves. If awakened by the reindeer, he\nharnesses each of them to his sleigh, delivers toys with them and finally unharnesses them\n(allowing them to go off on holiday). If awakened by a group of elves, he shows each of\nthe group into his study, consults with them on toy R\u0026D and finally shows them each out\n(allowing them to go back to work). Santa should give priority to the reindeer in the case\nthat there is both a group of elves and a group of reindeer waiting.\n```\n\nMy implementation is a clone of S. P. Jones' Haskell implementation, but, using my STM\nlibrary. There will be modifications and I'll note those modifications in the source code\nand here.\n\n## Known deviations\n\n* I used Go's channels for selection. In the Haskell code, S. P. Jones, uses `choose` to\n  choose reindeer and elven groups.\n\n```haskell\nsanta :: Group -\u003e Group -\u003e IO ()\nsanta elf_gp rein_gp = do\n    putStr \"----------\\n\"\n    choose [(awaitGroup rein_gp, run \"deliver toys\"),\n            (awaitGroup elf_gp, run \"meet in my study\")]\n  where\n    run :: String -\u003e (Gate,Gate) -\u003e IO ()\n    run task (in_gate,out_gate) = do\n        putStr (\"Ho! Ho! Ho! let’s \" ++ task ++ \"\\n\")\n        operateGate in_gate\n        operateGate out_gate\n```\n\nI emulate the same by using Go's channels:\n\n```Go\n// My Shitty Go code :/\n\n// for making the transactions for reindeers and elves\nfunc Santa(elfGroupCell *stm.MemoryCell, reindeerGroupCell *stm.MemoryCell) (workReindeers *stm.Transaction, reindeerChannel chan [2]*stm.MemoryCell, workElves *stm.Transaction, elfChannel chan [2]*stm.MemoryCell) {\n\treindeerChannel = make(chan [2]*stm.MemoryCell) // channel holding the reindeer gates\n\telfChannel = make(chan [2]*stm.MemoryCell)      // channel holding the elven gates\n\t//# work those reindeers Santa!\n\tworkReindeers = MySTM.NewT().\n\t\tDo(func(t *stm.Transaction) bool {\n\t\t\tinGateCell, outGateCell := AwaitGroup(reindeerGroupCell)\n\t\t\treindeerChannel \u003c- [2]*stm.MemoryCell{inGateCell, outGateCell} // dump into the reindeer channel\n\t\t\treturn true\n\t\t}).\n\t\tDone()\n\t//# work those reindeers Santa!\n\t//# work those elves Santa!\n\tworkElves = MySTM.NewT().\n\t\tDo(func(t *stm.Transaction) bool {\n\t\t\tinGateCell, outGateCell := AwaitGroup(elfGroupCell)\n\t\t\telfChannel \u003c- [2]*stm.MemoryCell{inGateCell, outGateCell} // dump into the elf channel\n\t\t\treturn true\n\t\t}).\n\t\tDone()\n\t//# work those elves Santa!\n\treturn workReindeers, reindeerChannel, workElves, elfChannel\n}\n\n// For running the transactions\n//# RUN!\nMySTM.ForkAndExec(workReindeers, workElves)\n//# RUN!\n//# Selection logic\n// The selection logic, as per the problem description\n// reindeers are given higher precedence, hence I wait for them to write\n// to the reindeer channel before proceeding forward\n// then I slip in a thread sleep to give the elves some time to reassemble.\nif rGates := \u003c-reindeerChannel; len(rGates) == 2 {\n  MySTM.Log(\"Ho! Ho! Hoo! Let's go deliver some toys! :D\")\n  MySTM.Log(\"-------------------------------------------\")\n  for _, gatecell := range rGates {\n    OperateGate(gatecell) // operate the gates\n  }\n  time.Sleep(5 * time.Second) // for pretty printing\n}\nif eGates := \u003c-elfChannel; len(eGates) == 2 {\n  MySTM.Log(\"Ho! Ho! Hoo! Let's hold the meeting in the study! :D\")\n  MySTM.Log(\"----------------------------------------------------\")\n  for _, gatecell := range eGates {\n    OperateGate(gatecell) // operate the gates\n  }\n  time.Sleep(5 * time.Second) // just for pretty printing\n}\n//# Selection logic\n```\n\n* I used a failing transaction pattern to emulate S. P. Jones' `check` function. This\n  function blocks until the condition is satisfied:\n\n```Go\nfunc Check(expression func() bool) {\n\t// MySTM.Log(\"Goroutine#\", getGID())\n\tcheckingT := MySTM.NewT().\n\t\tDo(func(t *stm.Transaction) bool {\n\t\t\treturn expression() // the transaction succeeds if expression evaluates to true else it blocks\n\t\t}).\n\t\tDone()\n\tMySTM.Exec(checkingT)\n\t// MySTM.Log(\"Goroutine#\", getGID())\n}\n```\n\n### Caveats\n\n* Verbosity\n\n* Unable to emulate the IO Monadic behavior of Haskell\n\n* Random parks of the goroutines and freeze ups when Santa is run more than 2-3 times.\n\n* Very sad with GoLang's `goroutines` - unable to readily get metadata for debugging like\n  in case of Java is a big problem for me.\n\n### Conclusion\n\nI was able to generate the output like S. P. Jones' Haskell implementation, but, my code\nis ugly and inelegant :(\n\n[Output](./root.log):\n\n```text\n2017/12/02 15:15:52 Beginning Santa's workplace simulation ~~~\n2017/12/02 15:15:52 Ho! Ho! Hoo! Let's go deliver some toys! :D\n2017/12/02 15:15:52 -------------------------------------------\n2017/12/02 15:15:52 Reindeer 6 delivering toys\n2017/12/02 15:15:52 Reindeer 8 delivering toys\n2017/12/02 15:15:52 Reindeer 7 delivering toys\n2017/12/02 15:15:52 Reindeer 4 delivering toys\n2017/12/02 15:15:52 Reindeer 1 delivering toys\n2017/12/02 15:15:52 Reindeer 2 delivering toys\n2017/12/02 15:15:52 Reindeer 3 delivering toys\n2017/12/02 15:15:52 Reindeer 5 delivering toys\n2017/12/02 15:15:52 Reindeer 9 delivering toys\n2017/12/02 15:15:57 Ho! Ho! Hoo! Let's hold the meeting in the study! :D\n2017/12/02 15:15:57 ----------------------------------------------------\n2017/12/02 15:15:57 Elf 3 meeting in the study\n2017/12/02 15:15:57 Elf 1 meeting in the study\n2017/12/02 15:15:57 Elf 2 meeting in the study\n2017/12/02 15:16:02 Ho! Ho! Hoo! Let's go deliver some toys! :D\n2017/12/02 15:16:02 -------------------------------------------\n2017/12/02 15:16:02 Reindeer 8 delivering toys\n2017/12/02 15:16:02 Reindeer 4 delivering toys\n2017/12/02 15:16:02 Reindeer 3 delivering toys\n2017/12/02 15:16:02 Reindeer 2 delivering toys\n2017/12/02 15:16:02 Reindeer 6 delivering toys\n2017/12/02 15:16:02 Reindeer 1 delivering toys\n2017/12/02 15:16:02 Reindeer 9 delivering toys\n2017/12/02 15:16:02 Reindeer 5 delivering toys\n2017/12/02 15:16:02 Reindeer 7 delivering toys\n2017/12/02 15:16:07 Ho! Ho! Hoo! Let's hold the meeting in the study! :D\n2017/12/02 15:16:07 ----------------------------------------------------\n2017/12/02 15:16:07 Elf 4 meeting in the study\n2017/12/02 15:16:07 Elf 6 meeting in the study\n2017/12/02 15:16:07 Elf 5 meeting in the study\n2017/12/02 15:16:12 Ho! Ho! Hoo! Let's go deliver some toys! :D\n2017/12/02 15:16:12 -------------------------------------------\n2017/12/02 15:16:12 Reindeer 3 delivering toys\n2017/12/02 15:16:12 Reindeer 8 delivering toys\n2017/12/02 15:16:12 Reindeer 9 delivering toys\n2017/12/02 15:16:12 Reindeer 6 delivering toys\n2017/12/02 15:16:12 Reindeer 4 delivering toys\n2017/12/02 15:16:12 Reindeer 5 delivering toys\n2017/12/02 15:16:12 Reindeer 7 delivering toys\n2017/12/02 15:16:12 Reindeer 1 delivering toys\n2017/12/02 15:16:12 Reindeer 2 delivering toys\n2017/12/02 15:16:17 Ho! Ho! Hoo! Let's hold the meeting in the study! :D\n2017/12/02 15:16:17 ----------------------------------------------------\n2017/12/02 15:16:17 Elf 3 meeting in the study\n2017/12/02 15:16:17 Elf 1 meeting in the study\n2017/12/02 15:16:17 Elf 9 meeting in the study\n2017/12/02 15:16:22 Santa is done for the day. Bye Santa ~~~\n```\n\n### References\n\n[[1]](https://www.schoolofhaskell.com/school/advanced-haskell/beautiful-concurrency/4-the-santa-claus-problem)\nS. P. Jones, \"Beautiful concurrency\", ch. 4, [Online] Available.\nhttps://www.schoolofhaskell.com/school/advanced-haskell/beautiful-concurrency/4-the-santa-claus-problem\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsidmishraw%2Fsanta-claus-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsidmishraw%2Fsanta-claus-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsidmishraw%2Fsanta-claus-go/lists"}