Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Pangamini · Nov 11, 2021 at 01:00 PM · unsafe

Associating NativeArray with a Job manually

I am running a job, which (for some reasons) accesses a NativeArray through a pointer. Therefore, the safety system is unaware of the job accessing the array, which is what I am trying to fix. Is there some intended way to achieve this? I was looking into AtomicSafetyHandle, but can't see anything relevant. I'd expect some method that tells the array/safety handle to disallow access until some JobHandle has completed.

Currently, I have an idea for a workaround, to schedule a separate dummy job (that claims the disputed array) and schedule it with a dependency on the unsafe job. Is this sufficient?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Nov 15, 2021 at 11:29 PM

I am running a job, which (for some reasons) accesses a NativeArray through a pointer.

NativeArray is a pointer :T
A humble pointer in a fancy clothes.

I'd expect some method that tells the array/safety handle to disallow access until some JobHandle has completed.

This method exists - it's not part of the NativeArray/Unity.Collections but Unity.Jobs. It's Schedule ( JobHandle dependency ) which you mentioned.


Currently, I have an idea for a workaround, to schedule a separate dummy job (that claims the disputed array) and schedule it with a dependency on the unsafe job. Is this sufficient?

Dependency chain is confusing at first, but you will get the hang of it eventually.

You didn't describe the specific situation in great detail here but if by any chance you're scheduling the same job every frame and this job accesses the same NativeArray allocation then this job should include it's own job handle as dependency.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pangamini · Nov 15, 2021 at 11:55 PM 0
Share

Thanks for the answer, though maybe I should rephrase the problem. My job does not have a NativeArray field, it has an IntPtr field. This field is a native pointer to the NativeArray's content. While it is true that it's just a fancy dress, it's this dress that provides the safety system with the right information about the array usage. Which is not provided in my case. Now you're going to ask "why", so I am going to tell you - I have a variable number of different arrays as a job input, and I can't have an array of arrays

avatar image andrew-lukasik Pangamini · Nov 16, 2021 at 12:13 AM 0
Share

it provides the safety system with the right information about the array usage.

It provides a warning system but not a scheduling system (seems similar but aren't). And this scheduling system is what you need. It's pointer type agnostic - whenever it's an IJob with NativeArray or unsafe pointer, doesn't matter, you just need to schedule the work with dependencies in order.

I have a variable number of different arrays as a job input, and I can't have an array of arrays

Think about it like this: every array can has it's own JobHandle (stored in a hashmap/dictionary, for example). If you do that then you can combine them into a single one and here is your dependency for collision-free scheduling.

The main thing about native collections is that they provide warnings so the whole engine won't crash immediately on wrong ptr access (out of bounds). To manage pointer access in time we use JobHadles.
avatar image Pangamini andrew-lukasik · Nov 16, 2021 at 10:49 AM 0
Share

Well... If I schedule one job that has a NativeArray field, it will prevent that array to be used in any job scheduled, unless the second job has the first job as a dependency. This is the safety system I am talking about. Now, my first job does NOT have a NativeArray field, but an unsafe pointer to its content, which means that the safety system does NOT know about the job's RW access to this array, and does NOT prevent other jobs from being scheduled without a dependency to this job. I need to somehow tell the safety system about the job's RW access to the array, other than the standard reflection-based automatic detection. I assume that, using reflection, the job struct is inspected for NativeContainer fields, and, optionally moderated by the [ReadOnly] and [WriteOnly] attributes, links the array to the job, and I assume that this safety functionality is controlled by the AtomicSafetyHandle. I am looking for a workaround, since the reflection-based system can't detect the NativeArray access in the job (because I am only using IntPtr or unsafe pointer) and can't prevent non-dependent jobs from accessing the array during scheduling.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does Unity define a Preprocessor Directive for 'UNSAFE'? 0 Answers

Compiler options, unsafe 2 Answers

How to implement custom Native List 1 Answer

Trouble creating a C# UDP interface to a C++ app in Unity 2 Answers

NativeList.GetUnsafePtr() throwing The type 'T' must be a non-nullable value type 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges