{"action":"created","issue":{"url":"https://api.github.com/repos/WebAssembly/threads/issues/103","repository_url":"https://api.github.com/repos/WebAssembly/threads","labels_url":"https://api.github.com/repos/WebAssembly/threads/issues/103/labels{/name}","comments_url":"https://api.github.com/repos/WebAssembly/threads/issues/103/comments","events_url":"https://api.github.com/repos/WebAssembly/threads/issues/103/events","html_url":"https://github.com/WebAssembly/threads/issues/103","id":355309309,"node_id":"MDU6SXNzdWUzNTUzMDkzMDk=","number":103,"title":"Memory mapping","user":{"login":"Daniel-Abrecht","id":10375108,"node_id":"MDQ6VXNlcjEwMzc1MTA4","avatar_url":"https://avatars0.githubusercontent.com/u/10375108?v=4","gravatar_id":"","url":"https://api.github.com/users/Daniel-Abrecht","html_url":"https://github.com/Daniel-Abrecht","followers_url":"https://api.github.com/users/Daniel-Abrecht/followers","following_url":"https://api.github.com/users/Daniel-Abrecht/following{/other_user}","gists_url":"https://api.github.com/users/Daniel-Abrecht/gists{/gist_id}","starred_url":"https://api.github.com/users/Daniel-Abrecht/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Daniel-Abrecht/subscriptions","organizations_url":"https://api.github.com/users/Daniel-Abrecht/orgs","repos_url":"https://api.github.com/users/Daniel-Abrecht/repos","events_url":"https://api.github.com/users/Daniel-Abrecht/events{/privacy}","received_events_url":"https://api.github.com/users/Daniel-Abrecht/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-08-29T19:55:32Z","updated_at":"2018-08-29T22:34:50Z","closed_at":null,"author_association":"NONE","body":"At the moment, there is only one continuous memory which is either shared or not, which corresponds to either an ArrayBuffer or a SharedArrayBuffer. But I think this complicates things a lot, and some things that are used in some programs aren't possible to implement the way it is now. For example:\r\n\r\n * Thread local variables would become a lot simpler. We could just map a SharedArrayBuffer for the shared memory to one address segment of the memory, and a different (Shared)ArrayBuffer to another one for the thread local memory, and just use a different one for the latter for the memory of each instance. The linker could know at which address which section starts using a linker script, like with any other platform.\r\n\r\n * Most mmap, mremap, shm_open, etc. functions related to memory mapping and shared memory are currently impossible to implement as specified by POSIX. For example:\r\n    * Threads aren't the only way to write concurrent programs with shared memory, a program may want to allocate some anonymous shared memory with mmap and MAP_SHARED and then fork a few times. All forks would share this memory segment.\r\n    * Programs may want to mmap a file, possibly even the same one. The file doesn't even have to exist, it could have been created by open(...,O_TMPFILE) (any current unix system), shm_open(SHM_ANON) (freebsd only), memfd_create() (linux only), etc. and may have been transferred by a unix socket, etc.\r\n\r\nI'm mainly concerned about the things that aren't possible to implement the way it is now. As for the implementation, I would have something like this in mind:\r\n```\r\nvar threadlocal = new SharedArrayBuffer(0x100000);\r\nvar sharedmem = new SharedArrayBuffer(0x1000000);\r\n\r\nvar memory = new WebAssembly.MappedMemory({\r\n  memory: [\r\n    {buffer: threadlocal, offset: 0x0},\r\n    {buffer: sharedmem, offset: 0x10000000}\r\n  ],\r\n  page_size: WebAssembly.PAGESIZE_DEFAULT // or WebAssembly.PAGESIZE_SYSTEM_MINIMUM\r\n});\r\n```\r\n\r\nOf course, I don't know how difficult this would be for browsers to implement, but I'm pretty sure it's possible. However, I think it may make sense to make a few restrictions, like only allowing SharedArrayBuffers or something similar, for example.\r\n\r\nI don't really have time to work out the details, and it's probably not that important for most applications. It's just something I thought would be nice to have, so I wanted to share the Idea.\r\n\r\nEdit: This may be similar to #84, even though not necessarily the same."},"comment":{"url":"https://api.github.com/repos/WebAssembly/threads/issues/comments/417129040","html_url":"https://github.com/WebAssembly/threads/issues/103#issuecomment-417129040","issue_url":"https://api.github.com/repos/WebAssembly/threads/issues/103","id":417129040,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNzEyOTA0MA==","user":{"login":"conrad-watt","id":6474972,"node_id":"MDQ6VXNlcjY0NzQ5NzI=","avatar_url":"https://avatars2.githubusercontent.com/u/6474972?v=4","gravatar_id":"","url":"https://api.github.com/users/conrad-watt","html_url":"https://github.com/conrad-watt","followers_url":"https://api.github.com/users/conrad-watt/followers","following_url":"https://api.github.com/users/conrad-watt/following{/other_user}","gists_url":"https://api.github.com/users/conrad-watt/gists{/gist_id}","starred_url":"https://api.github.com/users/conrad-watt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/conrad-watt/subscriptions","organizations_url":"https://api.github.com/users/conrad-watt/orgs","repos_url":"https://api.github.com/users/conrad-watt/repos","events_url":"https://api.github.com/users/conrad-watt/events{/privacy}","received_events_url":"https://api.github.com/users/conrad-watt/received_events","type":"User","site_admin":false},"created_at":"2018-08-29T22:34:50Z","updated_at":"2018-08-29T22:34:50Z","author_association":"CONTRIBUTOR","body":"Ah, I didn't properly get how MAP_SHARED works, and was focusing to much on the first bullet point, which only requires statically shared/local segments.\r\n\r\nIt's interesting to think of how this would work with exporting a Wasm memory to JS. What would the expectations be, I wonder, around creating a {Shared?}ArrayBuffer out of map_shmem (from the future features link above) mapped memory? The sketch above seems to suggest a new top-level memory type, \"mapped\", alongside \"shared\", and regular, which exposes segments as separate components."}}
{"actor":{"display_login":"conrad-watt"}}